Help with a list variable
- From: "Erich93063" <erich93063@xxxxxxxxx>
- Date: 3 Aug 2005 10:38:43 -0700
I have made the following test stored proc that all it does right now
is accept a string variable that will be a list ie. :5,9,6,13. Right
now all the SP is doing is creating a temp table to store the values in
the list, then looping through the list and inserting the values into
the temp table, then selecting all the records from teh temp table.
It's WORKING however i must have done something wrong because it's not
inserting ALL the values. If the list is "5,9,6,13" only 5, 9 and 6
will get inserted, not the 13. if I pass in the list like this
"5,9,6,13," with an extra comma at the end, they all get inserted fine
but thats not how the strings will be coming in. Here's what I have so
far:
CREATE PROCEDURE sp_searchTaskTest
-- in params
@strAssignedTo varchar (200) = NULL
AS
CREATE TABLE #tblAssignedTo (
strAssignedToID varchar(10)
)
-- initialize variables
DECLARE @lengthOfString int
DECLARE @startingPosition int
DECLARE @parseString1 int
DECLARE @strAssignedToID varchar(10)
SET @startingPosition = 0
SELECT @parseString1 = CHARINDEX (',', @strAssignedTo,1)
WHILE ( @parseString1 > 0 )
BEGIN
SELECT @parseString1 = CHARINDEX (',',
@strAssignedTo,@startingPosition)
SET @lengthOfString = @parseString1 - @startingPosition
IF @lengthOfString > 0
BEGIN
SET @strAssignedToID = SUBSTRING(@strAssignedTo, @startingPosition,
@lengthOfString)
SET @startingPosition = @parseString1 + 1
END
ELSE
BEGIN
SET @parseString1 = 0
SET @strAssignedToID = ''
END
IF @strAssignedToID != ''
BEGIN
INSERT #tblAssignedTo(strAssignedToID)
VALUES(@strAssignedToID)
END
END
SELECT * FROM #tblAssignedTo
GO
.
- Follow-Ups:
- Re: Help with a list variable
- From: Hugo Kornelis
- Re: Help with a list variable
- From: Chandra
- Re: Help with a list variable
- Prev by Date: Moving the distribution database
- Next by Date: Re: Moving the distribution database
- Previous by thread: Moving the distribution database
- Next by thread: Re: Help with a list variable
- Index(es):
Relevant Pages
|