Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute a dynamic variable in a stored procedure 3

Status
Not open for further replies.

tonys123

IS-IT--Management
Apr 30, 2007
46
GB
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 think you are making this a lot harder than it needs to be.

First, the reason you are having a problem is because of the dynamic SQL you are trying to execute it, and the way you are declaring the parameter.

You are calling sp_executesql with a named parameter, but the parameter is not used in the query. Try this instead.

Code:
DECLARE @imp nvarchar(500)
DECLARE @srvName nvarchar(500)
DECLARE @var nvarchar(500)
SET @imp='SELECT [!]@srvName = [/!]@@SERVERNAME'
EXECUTE sp_executesql
@imp,
N'@srvName nvarchar OUTPUT',
@srvName OUTPUT
select @srvName

When I run this on my instance of SQL server, I get... S as the output. So then I noticed that you are declaring @srvName in the sp_excutesql line as nvarchar, but without a size. Now try this:

Code:
DECLARE @imp nvarchar(500)
DECLARE @srvName nvarchar(500)
DECLARE @var nvarchar(500)
SET @imp='SELECT [!]@srvName = [/!]@@SERVERNAME'
EXECUTE sp_executesql
@imp,
N'@srvName nvarchar[!](500)[/!] OUTPUT',
@srvName OUTPUT
select @srvName

Now you should get you server name in the @srvName variable.

Now... I don't know if you simplified the code a lot for the purposes of posting this question. Lots of people do because they want to change variable names and such. I completely understand when this happens. However, if this is truly the code you want to execute, you can do so without using dynamic SQL, like this:

Code:
if @@SERVERNAME = 'SERVER123' -- whatever @@SERVERNAME returns
	select name from user_details where name like 'S%'
else
	select name from user_details where name like 'A%'

or like this:


Code:
select name 
from   user_details 
where  (@@SERVERNAME = 'SERVER123' And name like 'S%')
       or
       (name like 'S%')

or even this:

Code:
Declare @NameVariable varchar(100)

if @@SERVERNAME = 'SERVER123' -- whatever @@SERVERNAME returns
	Set @NameVariable = 'S'
Else
	Set @NameVariable = 'A'

select name 
from   user_details 
where  name like @NameVariable + '%'



-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hi Guys

Many thanks to you both - both approaches worked for me :)

Cheers

Anthony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top