If Exists and openquery

I have an open query dynamic query that works (attached)

How dow I do an If Exists on that?
In otherwords if nothing gets returned...
do something
else
do something else
Declare @sql varchar(4000)
Set @sql = 'select * from openquery(WEBSITE,''select * from web_leads where id=' + cast(@id as varchar(4)) + ' and available=False'')'
exec (@sql)

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?
 
JestersGrindConnect With a Mentor Commented:
I would insert the results into a temp table and then evaluate if the temp table has records or not.

Greg

0
 
Larry Bristersr. DeveloperAuthor Commented:
Thants what I did.  Thanks.

Here's the final code

Declare @msg varchar(10)
begin
Select t.* into #temp from (select * from openquery(WEBSITE,'select * from web_leads')) t;
 If exists (Select tt.id from #temp tt where tt.id=@id and tt.AVAILABLE = 0)
      Begin
            Select @msg = 'sorry'
      End
      Else
      Begin      
            Declare @sql varchar(4000)
            Set @sql = 'update openquery(WEBSITE,''select * from web_leads where id=' + cast(@id as varchar(4)) + ' and available=1'')' + 'set AVAILABLE = 0, USERNAME=''' + @username+''';'
            exec (@sql);
            Select @msg = 'updated'
      End      
End
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.