Solved

Parse multi-column array in sql stored procedure using a pivot (numbers) table

Posted on 2007-11-13
6
1,262 Views
Last Modified: 2008-01-09
In order to parse an array passed in as a parameter in a stored procedure I use a stored procedure ripped from here: http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm
To be more precise, it is method 5:
SELECT o.OrderID, CustomerID, EmployeeID, OrderDate
      FROM dbo.Orders AS o
      JOIN
      (
            
            SELECT LTRIM(RTRIM(SUBSTRING(OrderID, number+1, CHARINDEX(',', OrderID, number+1)-number - 1))) AS OrderID
            FROM      
            (
                  SELECT ',' + @OrderList + ',' AS OrderID
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.OrderID)
            WHERE SUBSTRING(OrderID, number, 1) = ','
      ) as Derived
      ON o.OrderID = Derived.OrderID
I use this sp to insert records in a table and instead of one column per insert (row) I need to do more.
For example, my input string would be something like:
col1_val1,col2_val1,col3_val1,col1_val2,col2_val2,col3_val2,col1_val3,col2_val3,col3_val3 ....
However, I can't get my head around how to build this query. I have tried a cross join to multiple Numbers table but that generates a cartesian product (I cannot get a distinct-like result).
Is there any way around it?
Thanks
0
Comment
Question by:daniel_balla
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20278914
Hi,
First create dbo.SplitOrderIDs specified in the method 6
 http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

Then just try this:

(I have used orderid,ShipVia,EmployeeID as three columns ).



declare @OrderList varchar(1000),@Col1 varchar(1000),@Col2 varchar(1000),@Col3 varchar(1000)
SET @OrderList ='10248,3,5,10259,3,4'


drop table #newtable

Select  IDENTITY(int, 1,1) AS ID_Num, OrderID INTO  #NewTable from dbo.SplitOrderIDs(@OrderList)
select * from #NewTable

Select @col1 = '',@col2 = '',@Col3 =''

select @Col1 =@Col1+cast(OrderID as varchar(10))+',' from #NewTable where Id_Num % 3 = 1
select @Col2 =@Col2+cast(OrderID as varchar(10))+',' from #NewTable where Id_Num % 3 = 2
select @Col3 =@Col3+cast(OrderID as varchar(10))+',' from #NewTable where Id_Num % 3 = 0

SELECT Distinct o.OrderID, CustomerID, EmployeeID, OrderDate
      FROM dbo.Orders AS o
      JOIN
      (
           
            SELECT LTRIM(RTRIM(SUBSTRING(Column1, number+1, CHARINDEX(',', Column1, number+1)-number - 1))) AS Column1
            FROM      
            (
                  SELECT ',' + @col1 + ',' AS Column1
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.Column1)
            WHERE SUBSTRING(Column1, number, 1) = ','

      ) as Derived1

      ON o.OrderID = Derived1.Column1

      JOIN
      (
           
            SELECT LTRIM(RTRIM(SUBSTRING(Column2, number+1, CHARINDEX(',', Column2, number+1)-number - 1))) AS Column2
            FROM      
            (
                  SELECT ',' + @col2 + ',' AS Column2
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.Column2)
            WHERE SUBSTRING(Column2, number, 1) = ','

      ) as Derived2

      ON o.ShipVia = Derived2.Column2
 JOIN
      (
           
            SELECT LTRIM(RTRIM(SUBSTRING(Column3, number+1, CHARINDEX(',', Column3, number+1)-number - 1))) AS Column3
            FROM      
            (
                  SELECT ',' + @col3 + ',' AS Column3
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.Column3)
            WHERE SUBSTRING(Column3, number, 1) = ','

      ) as Derived3

      ON o.EmployeeID = Derived3.Column3



0
 
LVL 9

Author Comment

by:daniel_balla
ID: 20284325
Fair enough, but as far as I can see the idea is to use the pivot table instead of the expensive string parsing (such as the specified UDF does) to optimize the whole process.
How is the proposed method any better than just parsing the string (using a while and the substring) and doing the inserts in the while loop?
do you reckon there is no way it can be done just using the join to the numbers table (so no loop around to parse the string)?
0
 
LVL 6

Accepted Solution

by:
Rajesh_mj earned 500 total points
ID: 20287378
Hi,
Just try: (Hope that it is some thing more better)

Declare @OrderList varchar(1000)
SET @OrderList ='10248,3,5,10259,3,4'

SELECT Distinct o.OrderID, CustomerID, EmployeeID, OrderDate
      FROM dbo.Orders AS o
      JOIN
      (
           
            SELECT LTRIM(RTRIM(SUBSTRING(OrderID, number+1, CHARINDEX(',', OrderID, number+1)-number - 1))) AS OrderID,
(LEN(SUBSTRING(OrderID,1,number+1)) -
            LEN(REPLACE(SUBSTRING(OrderID,1,number+1), ',', ''))) /
            LEN(',') as Num

            FROM      
            (
                  SELECT ',' + @OrderList + ',' AS OrderID
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.OrderID)
            WHERE SUBSTRING(OrderID, number, 1) = ','
      ) as Derived
      ON o.OrderID = CASE WHEN Derived.Num % 3 = 1 THEN Derived.OrderID END
 JOIN
      (
           
            SELECT LTRIM(RTRIM(SUBSTRING(OrderID, number+1, CHARINDEX(',', OrderID, number+1)-number - 1))) AS OrderID,
(LEN(SUBSTRING(OrderID,1,number+1)) -
            LEN(REPLACE(SUBSTRING(OrderID,1,number+1), ',', ''))) /
            LEN(',') as Num

            FROM      
            (
                  SELECT ',' + @OrderList + ',' AS OrderID
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.OrderID)
            WHERE SUBSTRING(OrderID, number, 1) = ','
      ) as Derived1
      ON o.ShipVia = CASE WHEN Derived1.Num % 3 = 2 THEN Derived1.OrderID END
JOIN
      (
           
            SELECT LTRIM(RTRIM(SUBSTRING(OrderID, number+1, CHARINDEX(',', OrderID, number+1)-number - 1))) AS OrderID,
(LEN(SUBSTRING(OrderID,1,number+1)) -
            LEN(REPLACE(SUBSTRING(OrderID,1,number+1), ',', ''))) /
            LEN(',') as Num

            FROM      
            (
                  SELECT ',' + @OrderList + ',' AS OrderID
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.OrderID)
            WHERE SUBSTRING(OrderID, number, 1) = ','
      ) as Derived2
      ON o.EmployeeID = CASE WHEN Derived2.Num % 3 = 0 THEN Derived2.OrderID END
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20287390
Hi,
Using temp table:
Declare @OrderList varchar(1000)
SET @OrderList ='10248,3,5,10259,3,4'


            SELECT LTRIM(RTRIM(SUBSTRING(OrderID, number+1, CHARINDEX(',', OrderID, number+1)-number - 1))) AS OrderID,
(LEN(SUBSTRING(OrderID,1,number+1)) -
            LEN(REPLACE(SUBSTRING(OrderID,1,number+1), ',', ''))) /
            LEN(',') as Num
            INTO #tmp
            FROM      
            (
                  SELECT ',' + @OrderList + ',' AS OrderID
            ) AS InnerQuery
            JOIN
            Numbers n
            ON
            n.Number < LEN(InnerQuery.OrderID)
            WHERE SUBSTRING(OrderID, number, 1) = ','


SELECT Distinct o.OrderID, CustomerID, EmployeeID, OrderDate
      FROM dbo.Orders AS o
      JOIN
       #tmp as Derived
      ON o.OrderID = CASE WHEN Derived.Num % 3 = 1 THEN Derived.OrderID END
 JOIN
      #tmp as Derived1
      ON o.ShipVia = CASE WHEN Derived1.Num % 3 = 2 THEN Derived1.OrderID END
JOIN
     #tmp Derived2
      ON o.EmployeeID = CASE WHEN Derived2.Num % 3 = 0 THEN Derived2.OrderID END
0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20287401
Hi,
I have taken Count String Occurrence Function from
 http://www.sql-server-helper.com/functions/count-string.aspx
0
 
LVL 9

Author Comment

by:daniel_balla
ID: 20289935
Thanks Rajesh. This might be the magic I need. Let me give it a try.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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

20 Experts available now in Live!

Get 1:1 Help Now