Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to obtain a variable Server Account Name using an @SQl with an @variable_server_ name + 'master.dbo.xp.instance.regread ...'

$
0
0

Hi,

I am trying to make a flexible script to get the Service Account Name from several servers.

I am using "servername.master.dbo.xp_instance_regread.......", but I want to use a variable for the servername.

So I started doing this:

Declare @servername varchar(100)

Declare @SQL varchar(1000)

Declare @agtServAccName varchar(250)

set @servername = 'Server1'

set @sql = @servername + '.master.dbo.xp_instance_regread

N''HKEY_LOCAL_MACHINE'',

N""SYSTEM\CurrentControlSet\Services\SQLSERVERAGENT'',

N''ObjectName'',

@AgtServAccName Output,

N''no_output'''

Execute (@SQL)  --(This runs without problems)

Select @AgtServAccName  --  <-- This gives an error "Must declare variable @AgtServAccName'

So the variable @AgtServAccName is 'forgotten'  after the Execute statement...

How am I able to get a flexible script, which gives me the Service Account Name?

EDIT: the reason I use a variable for the servicename is, that I am running it on a central management server and not on the server with this servername itself.





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>