How do I pass multiple values thought One Variable

Posted on 2007-11-20
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 125 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 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