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
 Old Forums
 CLOSED - General SQL Server
 if not exists

Author  Topic 

casati74
Posting Yak Master

109 Posts

Posted - 2006-09-07 : 09:31:57
It' s possible to write this

if not exists (exec (@sql))


where
set @sql = ' select '+@ConditionField+' from '+@InvolvedTable+'where'+@ConditionField+'='+@ConditionValue

I recive a error on exec

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-07 : 09:37:41
Well... If you received an error, I think it is not possible.

Select the DYNAMIC SQL query into a temporary table and use EXISTS against the temporary table.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

casati74
Posting Yak Master

109 Posts

Posted - 2006-09-07 : 09:49:04
For my it's ok this solution but I recive a error message

declare @ConditionField nvarchar(50), @ConditionValue nvarchar(50), @InvolvedTable nvarchar(50), @CondValue nvarchar(50)
set @ConditionField = 'MaterialID'
set @CondValue = '3'
set @InvolvedTable = 'EAFAdditions'
if exists (select @ConditionField, @ConditionValue from @InvolvedTable where @CondValue = 3)
begin
print 'ciaio'
end


He said me i must declare variable @InvolvedTable on from clausule; why?????
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2006-09-07 : 09:56:07
you can't use a variable in the FROM clause like this. You need to use dynamic sql if you want to pass an argument to your sproc (a table name for example) that is then used in a the FROM clause of a query.



-ec
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-09-07 : 16:01:57
[code]
if exists (select @ConditionField, @ConditionValue from @InvolvedTable where @CondValue = 3)
begin
print 'ciao'
end
[/code]

ciao!

well put...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-08 : 03:13:13
Priceless!


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -