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
 General SQL Server Forums
 New to SQL Server Programming
 case statement in where clause

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2009-06-08 : 03:03:37
based on userid i need to select the table name for fbid



select * from Rev_fbp_posted_dim_tb_tmp2(nolock)
where fbid like
case when @USERID <> ('admin','1206') then select fbid from table1
else select fbid from table2
end

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-08 : 03:10:15
select * from Rev_fbp_posted_dim_tb_tmp2(nolock)
where fbid in
case when @USERID not in ('admin','1206') then select fbid from table1
else select fbid from table2
end


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-08 : 03:15:31
try these solutions

solution 1:

IF @userID <> ('admin','1206')
select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table1 b on b.fbid = a.fbid
else
select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table2 b on b.fbid = a.fbid

Solution 2:

select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table1 b on b.fbid = a.fbid
where @userID <> ('admin','1206')
union
select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table2 b on b.fbid = a.fbid
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-08 : 03:24:32
quote:
Originally posted by webfred

select * from Rev_fbp_posted_dim_tb_tmp2(nolock)
where fbid in
case when @USERID not in ('admin','1206') then select fbid from table1
else select fbid from table2
end


No, you're never too old to Yak'n'Roll if you're too young to die.



I think your query will not work when subquery returned more than one value..
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-08 : 03:42:01
quote:
Originally posted by raky

quote:
Originally posted by webfred

select * from Rev_fbp_posted_dim_tb_tmp2(nolock)
where fbid in
case when @USERID not in ('admin','1206') then select fbid from table1
else select fbid from table2
end


No, you're never too old to Yak'n'Roll if you're too young to die.



I think your query will not work when subquery returned more than one value..


And I think your query will bring up an error while mine is running because I am using IN.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-08 : 03:51:54
quote:
Originally posted by raky

try these solutions

solution 1:

IF @userID <> NOT IN ('admin','1206')
select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table1 b on b.fbid = a.fbid
else
select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table2 b on b.fbid = a.fbid

Solution 2:

select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table1 b on b.fbid = a.fbid
where @userID <> NOT IN ('admin','1206')
union
select * from Rev_fbp_posted_dim_tb_tmp2 as a
inner join table2 b on b.fbid = a.fbid




beside the <> / NOT IN syntax error, the 2 solution will gives different result.

For Solution 1, either one of the query will execute.

In Solution 2, the 2nd query (after the union) will always execute


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -