Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 complicate combination

Author  Topic 

noamg
Posting Yak Master

215 Posts

Posted - 2013-06-10 : 05:47:22
Dear All,

A store procedure get an input string,
like: ‘( 1 AND ( 2 OR 3 ) OR ( 2 AND 4) ) OR 4 .... ect'

When each number is a table with a column UserID.
So each number represents a list of UserIDs.
AND means using INNER JOIN, OR MEANS using UNION

The SP has to return a select distinct UserID ….
HOW TO SOLVE IT ?


Noam Graizer

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-06-10 : 06:14:23
why are you passing like this ‘( 1 AND ( 2 OR 3 ) OR ( 2 AND 4) ) OR 4 .... ect' ?
what is the purpose of SP here? can you explain clearly?

--
Chandu
Go to Top of Page

noamg
Posting Yak Master

215 Posts

Posted - 2013-06-10 : 06:51:28
it is very hard to explain. but this is the core business here.

Noam Graizer
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-06-10 : 07:33:56
>>return a select distinct UserID ….
In the first post 1, 2, 3, 4 are UserIds from one table.
Step1: 2 OR 3 --> Both records
Step2: 2 AND 4 --> if both exists then only those two records
Step3: 1 AND AboveRecords (output of above two conditions) --> If 1 is also exists then include those records in output
Step4: Step3 output OR 4 --> Step3 User Ids and 4 to be included in final output...

Is the above result you want?

--
Chandu
Go to Top of Page

noamg
Posting Yak Master

215 Posts

Posted - 2013-06-10 : 08:01:53
no.I need something like this:

select UserID
from T1 T1
inner join (
select UserID from T2
union
select UserID from T3
) as TT
on T1.UserID = TT.UserID
union
....



Noam Graizer
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2013-06-10 : 10:50:22
If your parameter is declared as a varchar(whatever), then you will have to parse it so that you can do your where clause correctly.

Can you show us the actual stored proc parameters declaration?








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-06-11 : 11:42:42
quote:
Originally posted by noamg

it is very hard to explain. but this is the core business here.

Noam Graizer

If you can't explin it, then how do you expect us to be able to help you?

See the links below on how to ask your question so we can help:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -