Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help with Dynamic Query Results Expand / Collapse
Author
Message
Posted Monday, October 7, 2013 7:55 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 6:58 AM
Points: 18, Visits: 150
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
Southern Insurance Underwriters, Inc.

'...if they take my stapler then I'll set the building on fire...'
Post #1502143
Posted Monday, October 7, 2013 9:02 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, September 19, 2014 3:37 AM
Points: 708, Visits: 3,288
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

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
Post #1502191
Posted Monday, October 7, 2013 9:13 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 6:58 AM
Points: 18, Visits: 150
You rock! Thanks. I see where I went wrong and it's a perfect solution. Thanks again!

Frederick (Fred) J. Stemp, Jr.
Database Administrator
Southern Insurance Underwriters, Inc.

'...if they take my stapler then I'll set the building on fire...'
Post #1502207
Posted Monday, October 7, 2013 9:49 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, September 19, 2014 3:37 AM
Points: 708, Visits: 3,288
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

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
Post #1502230
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse