[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1085
  • Last Modified:

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

0
lrbrister
Asked:
lrbrister
1 Solution
 
JestersGrindCommented:
I would insert the results into a temp table and then evaluate if the temp table has records or not.

Greg

0
 
lrbristerAuthor 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now