Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TSQL @Var with multiple lines to table

Posted on 2012-03-26
5
Medium Priority
?
348 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 1400 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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

877 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