Solved

TSQL @Var with multiple lines to table

Posted on 2012-03-26
5
340 Views
Last Modified: 2012-03-26
Hi,

I have a storage Procedure that receives a Parameter (@Par2) with a string with multiple lines.

each line contain a serial number which is alphanumeric.
How can I run an insert a line into a table per each line on the @Par2

Example  the @Par2 as the following information:
APA7NB10A2900061
APA7NB10A2900062
APA7NB10A2900063
APA7NB10A2900064
APA7NB10A2900065
APA7NB10A2900066

I want to insert each of these lines into a #TEMP table with 1 column only

I know this cant be done with a BULK INSERT but honestly not sure how to parse the text per line.

thanks
0
Comment
Question by:Leitao
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
OCDan earned 350 total points
ID: 37767709
If you have a common element in your serial number e.g. start with AP or they have a space between them then you can use this function:
CREATE FUNCTION [dbo].[Split]
(    
    @RowData NVARCHAR(MAX),
    @Delimeter NVARCHAR(MAX)
)
RETURNS @RtnValue TABLE 
(
    ID INT IDENTITY(1,1),
    Data NVARCHAR(MAX)
) 
AS
BEGIN 
    DECLARE @Iterator INT
    SET @Iterator = 1

    DECLARE @FoundIndex INT
    SET @FoundIndex = CHARINDEX(@Delimeter,@RowData)

    WHILE (@FoundIndex>0)
    BEGIN
        INSERT INTO @RtnValue (data)
        SELECT 
            Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))

        SET @RowData = SUBSTRING(@RowData,
                @FoundIndex + DATALENGTH(@Delimeter) / 2,
                LEN(@RowData))

        SET @Iterator = @Iterator + 1
        SET @FoundIndex = CHARINDEX(@Delimeter, @RowData)
    END
    
    INSERT INTO @RtnValue (Data)
    SELECT Data = LTRIM(RTRIM(@RowData))

    RETURN
END

Open in new window


source: http://stackoverflow.com/questions/2954850/sql-to-return-a-table-based-on-string-input
0
 

Author Comment

by:Leitao
ID: 37767878
The only common element is the end of the line (CRLF).
the Serial number can be completely different and with different length , depending on the type of the product that the operator is working.
example, if is router they might start by M70***** or D90*****, if is a device will be 130****(Len = 12 or 13)

thanks
PL
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37767891
That is still ok, just embed the delimeter as char(13) or char(10). They are carriage return and line feed respectively, so they should work perfectly.
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 150 total points
ID: 37767909
DECLARE @Par2 VARCHAR(MAX) = 'APA7NB10A2900061
APA7NB10A2900062
APA7NB10A2900063
APA7NB10A2900064
APA7NB10A2900065
APA7NB10A2900066'
DECLARE @Delimiter VARCHAR(2) = char(10)
;WITH
  E1(N) AS (
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
           ),                          
  E2(N) AS (SELECT 1 FROM E1 a, E1 b),
  E4(N) AS (SELECT 1 FROM E2 a, E2 b)
 ,Numbers AS (SELECT TOP (2000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))  AS n FROM E4)

 SELECT SUBSTRING(@Delimiter+@Par2+@Delimiter,t.n+1,CHARINDEX(@Delimiter,@Delimiter+@Par2+@Delimiter,t.n+1)-t.n-1) AS Value
   FROM Numbers T
  CROSS JOIN (SELECT @Par2 AS C) mh
  WHERE t.n < LEN(@Delimiter+mh.c+@Delimiter)
    AND SUBSTRING(@Delimiter+mh.c+@Delimiter,t.n,1) = @Delimiter
0
 

Author Comment

by:Leitao
ID: 37768048
Thanks Guys.
Both solutions worked (with a copy paste) but OCDan was faster and I prefer it on the function :-)

Thank you for your very fast reactions
P.Leitao
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now