Author |
Topic |
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-02-10 : 03:52:13
|
if the dynamic SQL return any rows then only i need to perform some activity. how can i do this. and also how can i avoid dynamic sql..declare @TSQL varchar(max)declare @SQL varchar(max)declare @rowcnt intset @sql='select * from table1 where ' + conditionset @TSQL ="select @rowcnt =count(sysid) from ( + @sql + )exec(@TSql)if (@rowcnt>0 )begin exec(@sql)..do some thing..end========================================Project Manager who loves to code.===============Ask to your self before u ask someone |
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-10 : 04:02:17
|
declare @rowcnt intselect @rowcnt = count(s.sysid) from ( select * from table1 where condition )sif (@rowcnt>0 )beginselect * from table1 where condition ..do some thing..end |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-02-10 : 04:12:52
|
thanks nageswarbut i am trying to do this dynamic sql... my query is not fixed... i dont mind using dynamic SQL..========================================Project Manager who loves to code.===============Ask to your self before u ask someone |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-10 : 04:38:07
|
Dynamic SQL is not that efficientJai Krishna |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 06:05:51
|
how will condition values be passed? can you post a typical example value? |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-02-10 : 06:16:44
|
Hi VisakhI have this in SP. i have all comming in as parameters. my app has the UI where i build the where clause.. and depending on other parameter i change my qry by adding joins also.========================================Project Manager who loves to code.===============Ask to your self before u ask someone |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 06:28:54
|
that does not imply use of dynamic sql. give some sample of how values will be passed and whet you need as o/p in that case |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-02-10 : 08:46:51
|
I have a table which has some basic filter for a given roles. which is nothing but where clause.. i.e a person who delong to a division can only see his /her customer..i extract this and put in a variable.other that this user has ui to give his/her filter..this goes like parameter.one other parameter gives what table need to be joined. how do i get the @rowcnt value in below.. codedeclare @TSQL varchar(max)declare @SQL varchar(max)declare @rowcnt intset @sql='select * from table1 where ' + conditionset @TSQL ="select @rowcnt =count(sysid) from ( + @sql + )exec(@TSql)if (@rowcnt>0 )beginexec(@sql)..do some thing..end========================================Project Manager who loves to code.===============Ask to your self before u ask someone |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 09:33:43
|
still you didnt give what i asked. what will typical values passed for condition variable? |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-02-10 : 11:35:27
|
[code]DECLARE @SQL nvarchar(4000) ,@rowcnt intSET @SQL = '<Your select statement>; SET @rowcnt = @@ROWCOUNT'EXEC sp_executesql @SQL, N'@rowcnt int OUTPUT', @rowcnt OUTPUTSELECT @rowcnt[/code] |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-02-10 : 12:14:45
|
thanks ifor..this serves my purpose. but it also return the rows of the select . can we avoid this....i just need the row count.visakh16 its in big process.. bit hard to build simple workign example. sorry yar. if you can help me in above what ifor has given that great.========================================Project Manager who loves to code.===============Ask to your self before u ask someone |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-02-11 : 05:50:33
|
SET @SQL = 'SELECT @Rowcnt = COUNT(*) FROM...' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-11 : 08:47:24
|
quote: Originally posted by khalik thanks ifor..this serves my purpose. but it also return the rows of the select . can we avoid this....i just need the row count.visakh16 its in big process.. bit hard to build simple workign example. sorry yar. if you can help me in above what ifor has given that great.========================================Project Manager who loves to code.===============Ask to your self before u ask someone
i'm trying for a way to avoid dynamic sql. thats why i asked how condition will be passed |
|
|
|