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 2005 Forums
 Transact-SQL (2005)
 Execute sql statement!

Author  Topic 

moodi_z
Starting Member

38 Posts

Posted - 2009-05-27 : 03:46:09
Hi,
I need to check if a variable (@value='') is exists in a query result (@sql = 'select ........')

I thought to check if there is a where clause to that query, if not to add a where clause with the value, but the problem that I don't know what is that field:
if substring(@sql,charindex('where',@sql),5) = 'where

So I tried another way:

set @sql = 'select lineID from tbl_Lines'
set @sql = substring(@sql,0,charindex('from',@sql)) + 'into #tmp ' + substring(@sql,charindex('from',@sql),len(@sql))
execute (@sql)

but there is no #tmp table created!

isn't there a smart way to do it, something like:

If Exists(select @value IN (execute(@sql)) ?!!!!!

Thanks in advance.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-27 : 03:55:33
Yes, the #tmp table is created in the scope of the EXEC statement.
When EXEC returns, the temp table is destroyed.

A clever way to do the substition in string can be

set @sql = 'select lineID from tbl_Lines'
set @sql = replace(@sql, ' from ', ' into #tmp from '
execute(@sql)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

moodi_z
Starting Member

38 Posts

Posted - 2009-05-28 : 01:20:29
Thanks for your help.
Go to Top of Page
   

- Advertisement -