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 and TOP

Author  Topic 

aoriju
Posting Yak Master

156 Posts

Posted - 2011-04-20 : 08:27:36

DECLARE @str VARCHAR(100);

SET @str ='SR'



IF @str ='SR' THEN i want get

SELECT TOP 10 COL FROM TBL

ELSE

SELECT * FROM TBL


I am looking a Query with out IF and Dynamic Query

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-20 : 08:34:27
[code]
DECLARE @str VARCHAR(100);

SET @str ='SR'


IF @str ='SR' THEN i want get

SELECT TOP 10 COL FROM TBL

ELSE

SELECT * FROM TBL[/code]


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

Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2011-04-20 : 08:43:07
Hai...I don't like to use IF And ELSE statement....Is any other way to write this ?
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-20 : 09:09:09
No. You asked for two different queries - different conditions and different column lists. Why would you expect to combine them with 1 statement?

Corey

I Has Returned!!
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-20 : 09:11:10
what version of SQL Server are you using ?


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

Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2011-04-21 : 00:32:06
SQL 2008
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-21 : 00:45:46
[code]
DECLARE @str VARCHAR(100);

SET @str ='SR'

; with tbl as
(
select *, row_no = row_number() over (order by [somecol])
from [yourtable]
)
select *
from tbl
where @str <> 'SR'
or (@str = 'SR' and row_no <= 10)
[/code]


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

Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2011-04-21 : 01:05:24
Thanks very much
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-04-21 : 01:10:25
Are TCE's overused...do they beat temp or Global variable tables?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -