Solved

TSQL @Var with multiple lines to table

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Database Inventory 7 30
VMware PVSCSI SQL Server 2016 AlwaysOn 2 37
Report 8 27
SQL Server: Split string value in SELECT query only 5 29
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

751 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