Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TSQL @Var with multiple lines to table

Posted on 2012-03-26
5
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

704 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