jendeacott
Programmer
Hi,
I have a requirement to update mutiple rows in a table if their ItemID exists in a comma delimated field eg. 23,22,21
One method have been using is to use a split user defined fucntion that returns a resultset allowing me to use "in".
When I use the code below I get the "Subquery returned more than 1 value."
But If I change the Update to a select it displays the three records which I effectively want to update??
eg.
Can this method work with an update or is there a better way?
Any help would be great,
Thanks
Split UDF
Web Design Wetherby
Personal Web Design Service
I have a requirement to update mutiple rows in a table if their ItemID exists in a comma delimated field eg. 23,22,21
One method have been using is to use a split user defined fucntion that returns a resultset allowing me to use "in".
When I use the code below I get the "Subquery returned more than 1 value."
Code:
DECLARE @SWAPS varchar(5000)
SELECT @SWAPS= '12,55,33'
UPDATE ItemOffer Set OfferStatus = 'void'
where ItemID in (Select convert(int, Value) from dbo.split(@SWAPS, ','))
But If I change the Update to a select it displays the three records which I effectively want to update??
eg.
Code:
select * from ItemOffer
where ItemID in (Select convert(int, Value) from dbo.split(@SWAPS, ','))
Can this method work with an update or is there a better way?
Any help would be great,
Thanks
Split UDF
Code:
CREATE FUNCTION dbo.Split
(
@List nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Value nvarchar(100)
)
AS
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END
Web Design Wetherby
Personal Web Design Service