Need help with Dynamic Query Results

  • So my problem:

    I have to query Linked servers for Version number and evaluate it against another. This is going to be used in a SP that will do other stuff, not related to this issue. Also, the environment has MSSQL 2000 up to MSSQL 2008R2. I specifically want to identify the MSSQL 2000 servers, they are my issue in the next phase of my project.

    My solution (but not it seems):

    I have created the following Dynamic Query to hit the Linked servers:

    DECLARE @ServerName AS VARCHAR(50)

    SET @ServerName = <YOURSERVERNAME>

    DECLARE @Sql AS VARCHAR(100), @Result AS VARCHAR(10)

    SET @Sql = 'SELECT * FROM OPENQUERY([' + @ServerName + '], ' + '''' + 'SELECT SERVERPROPERTY(' + '''' + '''' + 'productversion' + '''' + '''' + ')' + '''' + ')'

    EXECUTE (@Sql) OUT, @RESULT OUTPUT

    IF @Result <= '9.00.1399.06'

    BEGIN

    PRINT 'Yes'

    END

    ELSE

    PRINT 'No'

    The first declare parameter @ServerName and PRINT statements are just to test my conditional results. @ServerName will be generated from a CURSOR later. I'm sure my issue has to do with the OUT and OUTPUT parameters, but I just can't seem to get my head unlocked from this one. Any help is greatly appreciated!

    Frederick (Fred) J. Stemp, Jr.
    Database Administrator / Database Developer
    Dealer Funding, LLC

    '...if they take my stapler then I'll set the building on fire...'

  • Here you go:

    DECLARE @ServerName AS NVARCHAR(50)

    SET @ServerName = <YOURSERVERNAME>

    DECLARE @SQL AS NVARCHAR(1000), @Result AS NVARCHAR(10)

    SET @SQL = 'SELECT @theResult = CAST(Result AS NVARCHAR(50)) FROM OPENQUERY([' + @ServerName + '], ' + '''' + 'SELECT SERVERPROPERTY(' + '''' + '''' + 'productversion' + '''' + '''' + ') AS Result' + '''' + ')'

    exec sp_executesql @SQL, N'@theResult NVARCHAR(50) output', @Result OUTPUT

    IF @Result <= '9.00.1399.06'

    BEGIN

    PRINT 'Yes'

    END

    ELSE

    PRINT 'No'

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • You rock! Thanks. I see where I went wrong and it's a perfect solution. Thanks again!

    Frederick (Fred) J. Stemp, Jr.
    Database Administrator / Database Developer
    Dealer Funding, LLC

    '...if they take my stapler then I'll set the building on fire...'

  • You're welcome 😀

    Just noticed you may want to change the size of your @Result variable to more than 10 characters as the version number you're checking is 12 characters long?

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply