SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help with Dynamic Query Results


Need help with Dynamic Query Results

Author
Message
Fred Stemp
Fred Stemp
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 219
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...'
Abu Dina
Abu Dina
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1249 Visits: 3323
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
Fred Stemp
Fred Stemp
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 219
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...'
Abu Dina
Abu Dina
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1249 Visits: 3323
You're welcome :-D

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search