Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How do I pass multiple values thought One Variable

Posted on 2007-11-20
Medium Priority
Last Modified: 2010-05-18

I have this stored procedure below which at present only filters on one variable @Product_Code1.

Its it possible to pass multiple values to this variable e.g.

exec test '1633,1891'

I've tried the above, but it interprets it as one string.
@Product_Code1 nvarchar(100)
SELECT     dbo.cart.Customer_URN, dbo.TRIM(dbo.customer.Customer_Title) As Title, dbo.TRIM(dbo.customer.Customer_Forename) As Forename, dbo.TRIM(dbo.customer.Customer_Surname) As Surname
FROM         dbo.cart INNER JOIN
                      dbo.cartitem ON dbo.cart.Cart_URN = dbo.cartitem.Cart_URN INNER JOIN
                      dbo.product ON dbo.cartitem.Product_URN = dbo.product.Product_URN INNER JOIN
                      dbo.customer ON dbo.cart.Customer_URN = dbo.customer.Customer_URN
WHERE     (dbo.cart.Cart_Order_DateTime BETWEEN CONVERT(DATETIME, '2007-01-15 00:00:00', 102) AND CONVERT(DATETIME, '2007-11-15 00:00:00', 102)) 
                      AND (dbo.cart.Cart_Status = N'Order') AND (dbo.product.Product_Code) in (@Product_Code1)
GROUP BY dbo.cart.Customer_URN, dbo.customer.Customer_Title, dbo.customer.Customer_Forename, dbo.customer.Customer_Surname, 
HAVING      (NOT (dbo.cart.Customer_URN IN
                          (SELECT     dbo.cart.Customer_URN
                            FROM          dbo.cart INNER JOIN
                                                   dbo.cartitem ON dbo.cart.Cart_URN = dbo.cartitem.Cart_URN INNER JOIN
                                                   dbo.product ON dbo.cartitem.Product_URN = dbo.product.Product_URN
                            WHERE      (dbo.cart.Cart_Order_DateTime BETWEEN CONVERT(DATETIME, '2007-10-15 00:00:00', 102) AND CONVERT(DATETIME, 
                                                   '2007-11-15 00:00:00', 102)) AND (dbo.cart.Cart_Status = N'ORDER')
                            GROUP BY dbo.cart.Customer_URN))) AND (dbo.customer.Customer_Marketing <> N'DEAD')
                            ORDER BY Surname

Open in new window

Question by:BenthamLtd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 20320891
What is the data type of your Product_Code field?

Author Comment

ID: 20320915
The data type is nvarchar

Accepted Solution

doobdave earned 500 total points
ID: 20321226
Hi there,

as I see it, you have two viable options:

1) use dynamic SQL.  This involves concatenating the string passed as a parameter into your select statement and using EXEC or sp_executesql to execute it (look those up in BOL). This is subject to SQL Injection attacks, can be poorly performing, and does not provide compile-time syntax checking

2) Create a table-valued function (see code snippet) to split a string into a table variable.  The table variable will contain a row for each value. You can then use your original SQL with an IN clause, using the function as follows:

SELECT ... FROM ... WHERE colname IN (SELECT SplitValues FROM dbo.ftn_DelimitedListToIntTableVariable)

Note that you will need to grant SELECT permission on the function.

I highly recommend option 2 as it's more flexible.

Best Regards,

CREATE FUNCTION [dbo].[ftn_DelimitedListToIntTableVariable]
	@DelimitedList varchar(max),
	@Delimiter varchar(1)
RETURNS @tbl table(SplitValues int)
	Accepts a character-delimited string of values.
	Returns a table variable, containing one row for each value in the string that was passed in.
	Values are split according to the character supplied for the @Delimiter variable.
	Values are converted to integer data type and stored in the returned table.
	DECLARE @DelimiterPos int
	-- Find the first comma
	SET @DelimiterPos = PATINDEX( '%,%', @DelimitedList)
	-- If a delimiter was found, @DelimiterPos will be > 0.
	WHILE @DelimiterPos > 0
			-- Insert the value between the start of the string and the first delimiter, into the table variable.
			INSERT INTO @tbl(SplitValues) SELECT CAST(LTRIM(RTRIM((SUBSTRING(@DelimitedList, 1, @DelimiterPos -1)))) AS int)
			-- Trim the string of the first value and delimiter.
			SET @DelimitedList = SUBSTRING(@DelimitedList, @DelimiterPos +1, LEN(@DelimitedList) - @DelimiterPos)
			-- Look for the next delimiter in the string.
			SET @DelimiterPos = PATINDEX( '%,%', @DelimitedList)
	-- Ensure the last / only value in the @DelimitedList string gets inserted into the table variable.
	INSERT INTO @Tbl(SplitValues) SELECT CAST(LTRIM(RTRIM((@DelimitedList))) AS int)

Open in new window


Expert Comment

ID: 20321237
sorry, I just noticed you're using SQL Server 2000, you will need to change the input paramter for the function to be varchar(8000) instead of varchar(max)

Author Comment

ID: 20323484
Thanks for your suggestion.

I have made a work around using a built table as the productcode selection.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question