Hi
I am trying to dynamically retrieve my server name and then, from that, run a statement which will vary depending on the server name that is returned. I can obtain the server name dynamically but am unable to pass it to my IF statement; regardless of everything, @srvName in my IF statement appears to be null so the ELSE part of the statement is the one that always runs. How do I get the IF statement to recognise the server name that is returned? An example of my code is:
DECLARE @imp nvarchar(500)
DECLARE @srvName nvarchar(500)
DECLARE @var nvarchar(500)
SET @imp='SELECT @@SERVERNAME'
EXECUTE sp_executesql
@imp,
N'@srvName nvarchar OUTPUT',
@srvName OUTPUT
select @srvName
if @srvName = 'SERVER123' -- whatever @@SERVERNAME returns
set @var= N'select name from user_details where name like ''S%'''
else
set @var= N'select name from user_details where name like ''A%'''
EXECUTE (@var)
Thanks
I am trying to dynamically retrieve my server name and then, from that, run a statement which will vary depending on the server name that is returned. I can obtain the server name dynamically but am unable to pass it to my IF statement; regardless of everything, @srvName in my IF statement appears to be null so the ELSE part of the statement is the one that always runs. How do I get the IF statement to recognise the server name that is returned? An example of my code is:
DECLARE @imp nvarchar(500)
DECLARE @srvName nvarchar(500)
DECLARE @var nvarchar(500)
SET @imp='SELECT @@SERVERNAME'
EXECUTE sp_executesql
@imp,
N'@srvName nvarchar OUTPUT',
@srvName OUTPUT
select @srvName
if @srvName = 'SERVER123' -- whatever @@SERVERNAME returns
set @var= N'select name from user_details where name like ''S%'''
else
set @var= N'select name from user_details where name like ''A%'''
EXECUTE (@var)
Thanks