May 18, 2009 at 9:59 am
hi gurus, i have a question, i have a stored procedure which gets execute every minute, which i have to convert all literal to variables. Example
Select distributorid, distributorname, distributorstatus,distributorcargo
from Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = '5'
and distributorstatus in ('9', '7','5')
final:
declare @cargo int
set @cargo = 5
Select distributorid, distributorname, distributorstatus,distributorcargo
from Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = @cargo
and distributorstatus in ('9', '7','5','1')
my issue is creating a variable for distributorstatus in ('9', '7','5','1')
i know i can create a table variable or a temp table, but due to some issues we have in prod i really would like to prevent that, is there another way?
May 18, 2009 at 10:09 am
There may be other alternatives, but the only two I can think of are to create a temorary (dynamic) table and join to it in the FROM clause or create ana execute a dynamic query inside the stored procedure.
May 18, 2009 at 10:26 am
declare @cargo int
set @cargo = 5
Select distributorid, distributorname, distributorstatus,distributorcargo
from Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = @cargo
and distributorstatus in ('9', '7','5','1')
You should consider the use of dynamic SQL, your variable might be of the varchar type e.g.
declare @status varchar(max),
@cargo int,
@sql varchar(max)
set @cargo = 5;
set @status='9,7,5,1';
set @status= replace (@status,',',''',''')
set @status='('''+@status+''')'
set @sql= 'Select distributorid, distributorname, distributorstatus,distributorcargo
from Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = '+ cast(@cargo as varchar)+'and distributorstatus in '+@status
exec(@sql)
May 18, 2009 at 10:30 am
FelixG (5/18/2009)
declare @cargo int
set @cargo = 5
Select distributorid, distributorname, distributorstatus,distributorcargo
from Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = @cargo
and distributorstatus in ('9', '7','5','1')
You should consider the use of dynamic SQL, your variable might be of the varchar type e.g.
declare @status varchar(max),
@cargo int,
@sql varchar(max)
set @cargo = 5;
set @status='9,7,5,1';
set @status= replace (@status,',',''',''')
set @status='('''+@status+''')'
set @sql= 'Select distributorid, distributorname, distributorstatus,distributorcargo
from Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = '+ cast(@cargo as varchar)+'and distributorstatus in '+@status
exec(@sql)
If you go the dynamic sql direction, be sure to validate the data being sent in to the procedure to protect against SQL INJECTION attacks.
May 18, 2009 at 11:34 am
I'm not sure why you'd need to move those values into variables, unless you are converting your query to one that takes paramamters. Additionally, using a split function to populat a temp table/table variable is probably your most effiecent option if you need to handle dynamic lists. But, there are other methods. Here is one way that you can avoid dynamic SQL, but I wouldn't use it in an sort of high volume or large table scenario: DECLARE @Foo TABLE (ID INT, StatusID INT)
INSERT @Foo
SELECT 1, 1
UNION ALL SELECT 2, 2
UNION ALL SELECT 3, 3
UNION ALL SELECT 4, 4
UNION ALL SELECT 5, 5
UNION ALL SELECT 6, 3
UNION ALL SELECT 7, 4
UNION ALL SELECT 8, 5
DECLARE @StatusList VARCHAR(4000)
SET @StatusList = ',' + '5,1,2' + ','
SELECT *
FROM @Foo
WHERE @StatusList LIKE '%,' + CAST(StatusID AS VARCHAR(15)) + ',%'
May 18, 2009 at 1:09 pm
DBA (5/18/2009)
Select distributorid, distributorname, distributorstatus,distributorcargofrom Dist_Main M inner join Dist_Details D on
M.DistPK = D.DistFK
where distribuborcargo = @cargo
and distributorstatus in ('9', '7','5','1')
my issue is creating a variable for distributorstatus in ('9', '7','5','1')
i know i can create a table variable or a temp table, but due to some issues we have in prod i really would like to prevent that, is there another way?
How about XML?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 18, 2009 at 2:29 pm
thanks, i will try your ways, just to give you some info these are sps that pass every minute and we were having issues with the caches.
thanks again.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply