If Exists and openquery

Posted on 2011-10-17
Last Modified: 2012-05-12
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
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

Question by:lrbrister
    LVL 21

    Accepted Solution

    I would insert the results into a temp table and then evaluate if the temp table has records or not.



    Author Closing Comment

    Thants what I did.  Thanks.

    Here's the final code

    Declare @msg varchar(10)
    Select t.* into #temp from (select * from openquery(WEBSITE,'select * from web_leads')) t;
     If exists (Select from #temp tt where and tt.AVAILABLE = 0)
                Select @msg = 'sorry'
                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'

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now