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 2000 Forums
 SQL Server Development (2000)
 Check if Dynamic SQL return rows...

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 int

set @sql='select * from table1 where ' + condition

set @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 int

select @rowcnt = count(s.sysid) from ( select * from table1 where condition )s

if (@rowcnt>0 )
begin
select * from table1 where condition
..do some thing..
end
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-02-10 : 04:12:52
thanks nageswar

but 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
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-10 : 04:38:07
Dynamic SQL is not that efficient

Jai Krishna
Go to Top of Page

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?
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-02-10 : 06:16:44
Hi Visakh

I 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
Go to Top of Page

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
Go to Top of Page

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.. code

declare @TSQL varchar(max)
declare @SQL varchar(max)
declare @rowcnt int
set @sql='select * from table1 where ' + condition
set @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
Go to Top of Page

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?
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-02-10 : 11:35:27
[code]DECLARE @SQL nvarchar(4000)
,@rowcnt int
SET @SQL = '<Your select statement>; SET @rowcnt = @@ROWCOUNT'

EXEC sp_executesql @SQL, N'@rowcnt int OUTPUT', @rowcnt OUTPUT

SELECT @rowcnt[/code]
Go to Top of Page

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
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-02-11 : 05:50:33
SET @SQL = 'SELECT @Rowcnt = COUNT(*) FROM...'
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -