1

I can someone help me turn this sql into a stored proc..

select * from voilets 
where cfrw = 'F16'
UNION 
(select * 
from voilets
where cfrw in ('B05','B12','R02','F01','F16','F17','U11','U03','U04','U21')) 
ORDER BY DSCA

Where 'F16 is a variable called @default and 'B05','B12','R02','F01','F16','F17','U11','U03','U04','U21' is an array of @voilets

This is not working for me:

@sCarrierSelect varchar(max)
AS
BEGIN

declare @SQL nvarchar(4000)

set @SQL = '

select * from voilets
where t_cfrw =  ' + @default + '
UNION 
(select * 
from carriers 
where t_cfrw in (' + @voilets+')) 
ORDER BY T_DSCA

'
print @SQL

exec sp_executesql @SQL


END
1
  • @marc unfortunatly the requirement is to use stored proc. Is there anyway to make the query run with a stored proc. I cant figure this one out :/ Commented Feb 24, 2012 at 2:52

4 Answers 4

3

IF you SQL Server IS >=2008 then:

USE tempdb;
GO

CREATE TABLE voilets
    (cfrw char(3), DSCA int)
go
INSERT INTO voilets VALUES ('R02', 2)
INSERT INTO voilets VALUES ('F16', 5)
INSERT INTO voilets VALUES ('F16', 4)
INSERT INTO voilets VALUES ('X77', 9)
go
CREATE TYPE myType AS TABLE (id CHAR(3));
GO
CREATE PROCEDURE usp_myProc
    @default char(3),
    @voiletsTVP myType READONLY
    AS 
select * from voilets 
where cfrw = @default
UNION 
(select * 
from voilets
where cfrw in (SELECT * FROM @voiletsTVP)) 
ORDER BY DSCA
GO
-------------------------
DECLARE @default char(3)
SET @default='F16'
DECLARE @voiletsTVP AS myType;
INSERT INTO @voiletsTVP SELECT * FROM (VALUES ('B05'),('B12'),('R02'),('F01'),('F16'),('F17'),('U11'),('U03'),('U04'),('U21')) q(x)
EXEC usp_myProc @default,@voiletsTVP
GO

Result-set:

cfrw    DSCA
R02     2
F16     4
F16     5
Sign up to request clarification or add additional context in comments.

Comments

1

Performing that safely in a sproc is actually quite tricky; there are a few common approaches:

  • use a udf to split a string on a token - google for "split udf" (there will be many), and join on the results
  • use a table valued parameter

Personally, I rarely use sprocs these days; I'd use dapper:

List<string> foo = ...
var items = conn.Query<SomeType>(
    "select * from [table] where colName in @foo", new { foo }).ToList();

Most LINQ providers and ORMs will have options here too, involving Contains etc.

Comments

1

You can learn about Passing Arrays in SQL Parameters using XML Data Type in SQL Server 2005

See sample:

/* for this xml:
<list>
    <item>42</item>
    <item>73</item>
    <item>2007</item>
</list>
*/

CREATE FUNCTION [lm].[SplitList]
(
    @list AS XML
)
RETURNS TABLE
AS
RETURN
(
    SELECT tempTable.item.value('.', 'VARCHAR(MAX)') AS Item
    FROM @list.nodes('list/item') tempTable(item)
);

Comments

0

Why not use a sql CLR function to split your values, passing those into your procedure. Here is a very good and fast split string implementation: CLR Split String. If you can't use sql clr, then look online for 'sql split string'. Whichever you use you put the result of that work into a temporary table and join that to your main table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.