Solved

TSQL @Var with multiple lines to table

Posted on 2012-03-26
5
337 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to convert JSON file to csv? 7 54
SQL Mirror and Replication 5 18
separate column 24 21
Date conversion in sql server 2012 6 26
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

746 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

13 Experts available now in Live!

Get 1:1 Help Now