Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SOLVED Insert based on comma/pipe delimited str

Author  Topic 

badman3k
Starting Member

15 Posts

Posted - 2009-04-01 : 10:30:29
I'm struggling to build a stored procedure that amongst other variables takes in a parameter 'items'. This is a single string that is firstly delimited with the pipe '|' (to indicate new row) and then comma ',' (to separate the columns).

So far I've managed to get a function together that takes a string and splits it into rows in a table:

	RETURNS @Strings TABLE (
position INT IDENTITY PRIMARY KEY
, value VARCHAR(8000)
)

AS BEGIN
DECLARE @index int
SET @index = -1

WHILE (LEN(@text) > 0) BEGIN
-- Find the first delimiter
SET @index = CHARINDEX(@delimiter , @text)

-- No delimiter left?
-- Insert the remaining @text and break the loop
IF (@index = 0) AND (LEN(@text) > 0) BEGIN
INSERT INTO @Strings VALUES (@text)
BREAK
END

-- Found a delimiter
-- Insert left of the delimiter and truncate the @text
IF (@index > 1) BEGIN
INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
-- Delimiter is 1st position = no @text to insert
ELSE SET @text = RIGHT(@text, (LEN(@text) - @index))
END
RETURN
END


Now from the above I get a 2xY table with each part of the string on a new row.

What I'm now struggling with is the insert part, as I need to insert each of the returned rows split on a comma, into the columns of the target table.

At present this is what my query looks like:
insert into order_items (order_id, qty, units, description, cost, invoice, invoice_id) values('10012', select value from fn_Split(select value from fn_Split(@items, '|'), ','), 'N', NULL)


The above fails... which was obvious. If anyone has any ideas on how to do this I'd appreciate some guidance - FYI SQL is not my forte, but I am learning quickly.

Many thanks in advance,

Rich

badman3k
Starting Member

15 Posts

Posted - 2009-04-01 : 11:44:37
Think I've solved it another way. Here's the code:
	declare @values varchar(255)
declare @iStart int, @iEnd int, @int_error int
set @int_error = 0
set @iStart = 0
set @iEnd = 0

while @iStart <= len(@p_items) - charindex('|', reverse(@p_items)) + 1
begin
set @iEnd = charindex('|', @p_items, @iStart + 1) - 1
if @iEnd - @iStart <= 0
set @iEnd = len(@p_items)+1
select @values = substring(@p_items, @iStart + 1, @iEnd - @iStart)
exec('insert into order_items (order_id, qty, units, description, cost, invoiced, invoice_id) values (' + @order_id + ', ' + @values + ', ''N'', NULL)')
set @int_error = @@error
if @int_error <> 0
set @items_err = @int_error
set @iStart = @iEnd + 1
if @iStart = 0
break
end


It requires that the @p_items be of the following form: '12,''ea'',''Test1'',12.34,''error'' | 13,''ea'',''Test2'',45.12 | 14,''ea'',''Test3'',12.12' -- don't forget to double ' the text variables.
Hopefully this will help someone else in the future, and thanks all those that have checked to see if they could help, appreciate you looking.

Edit: The original code wouldn't work inside a stored procedure, the indexes were wrong. The above will work, and also pick up anything from one to x lines, just make sure that the input string does not have the pipe at the end.

Rich
Go to Top of Page
   

- Advertisement -