Hi all,
Went looking on the net and found the following procedure that takes a comma seperated value as an input parameter and then processes it:
I am trying to modify the code so that it will take to parameters in CSV format: @group and @member. I am attempting to insert the data along with an organization number into a table that is of the following format:
memberID int (4), groupID int (4), and myOrg (254). The procedure now looks as follows:
The data I am inserting is: @member="1,251,3,4,1,251,3,4,1,251,3,4" and @group="6,6,6,6,7,7,7,7,9,9,9,9" and @OrgID="ST466C513"
The problem that is occuring is that the procedure is going into an infiniate loop and is stuck working on the first line of data and inserts @member=6, @group=6 and @orgID="ST466C513".
The second problem that arises is if the groupValue is greater that 10, the procedure is unable to perform the type cast when asked.
Any suggestions??
Mike55
Went looking on the net and found the following procedure that takes a comma seperated value as an input parameter and then processes it:
Code:
CREATE PROC dbo.GetOrderList2
(
@OrderList varchar(500)
)
AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #TempList
(
OrderID int
)
DECLARE @OrderID varchar(10), @Pos int
SET @OrderList = LTRIM(RTRIM(@OrderList))+ ,
SET @Pos = CHARINDEX(,, @OrderList, 1)
IF REPLACE(@OrderList, ,, ) <>
BEGIN
WHILE @Pos > 0
BEGIN
SET @OrderID = LTRIM(RTRIM(LEFT(@OrderList, @Pos - 1)))
IF @OrderID <>
BEGIN
INSERT INTO #TempList (OrderID) VALUES (CAST(@OrderID AS int)) --Use Appropriate conversion
END
SET @OrderList = RIGHT(@OrderList, LEN(@OrderList) - @Pos)
SET @Pos = CHARINDEX(,, @OrderList, 1)
END
END
SELECT o.OrderID, CustomerID, EmployeeID, OrderDate
FROM dbo.Orders AS o
JOIN
#TempList t
ON o.OrderID = t.OrderID
END
GO
I am trying to modify the code so that it will take to parameters in CSV format: @group and @member. I am attempting to insert the data along with an organization number into a table that is of the following format:
memberID int (4), groupID int (4), and myOrg (254). The procedure now looks as follows:
Code:
CREATE PROCEDURE dbo.UpdateTables (
@OrgID nvarchar(254),
@member varchar(254),
@group varchar(254)
)
AS
SET NOCOUNT ON
DECLARE @memberID varchar (10)
DECLARE @groupID varchar (10)
DECLARE @Pos1 int
DECLARE @Pos2 int
SET @member = LTRIM(RTRIM(@member))+ ,
SET @group = LTRIM(RTRIM(@group))+ ,
SET @Pos1 = CHARINDEX(,, @member, 1)
SET @Pos2 = CHARINDEX(,, @group, 1)
IF REPLACE(@member, ,, ) <> and REPLACE(@group, ,, ) <>
BEGIN
WHILE @Pos1 > 0 and @Pos2 > 0
BEGIN
SET @memberID = LTRIM(RTRIM(LEFT(@member, @Pos1 - 1)))
SET @groupId = LTRIM(RTRIM(LEFT(@group, @Pos2 - 1)))
IF @memberID <> and @groupID <>
BEGIN
INSERT INTO dataTable4 (memberID, groupID, myOrg) VALUES (CAST(@memberID AS int), CAST(@groupID AS int), @OrgID) --Use Appropriate conversion
END
SET @member = RIGHT(@member, LEN(@member) - @Pos1)
SET @Pos1 = CHARINDEX(,, @member, 1)
SET @member = RIGHT(@group, LEN(@group) - @Pos2)
SET @Pos2 = CHARINDEX(,, @group, 1)
END
END
GO
The data I am inserting is: @member="1,251,3,4,1,251,3,4,1,251,3,4" and @group="6,6,6,6,7,7,7,7,9,9,9,9" and @OrgID="ST466C513"
The problem that is occuring is that the procedure is going into an infiniate loop and is stuck working on the first line of data and inserts @member=6, @group=6 and @orgID="ST466C513".
The second problem that arises is if the groupValue is greater that 10, the procedure is unable to perform the type cast when asked.
Any suggestions??
Mike55