SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Old Forums
 CLOSED - General SQL Server
 if not exists
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

casati74
Posting Yak Master

Italy
109 Posts

Posted - 09/07/2006 :  09:31:57  Show Profile
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

Sweden
29910 Posts

Posted - 09/07/2006 :  09:37:41  Show Profile  Visit SwePeso's Homepage
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

Italy
109 Posts

Posted - 09/07/2006 :  09:49:04  Show Profile
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
Flowing Fount of Yak Knowledge

USA
3575 Posts

Posted - 09/07/2006 :  09:56:07  Show Profile
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

Sweden
3279 Posts

Posted - 09/07/2006 :  16:01:57  Show Profile

if exists (select @ConditionField, @ConditionValue from @InvolvedTable where @CondValue = 3)
begin
print 'ciao'
end


ciao!

well put...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 09/08/2006 :  03:13:13  Show Profile  Visit SwePeso's Homepage
Priceless!


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000