Solved

Row Insert

Posted on 2014-09-09
4
180 Views
Last Modified: 2014-09-09
I have the below temp table:

CREATE TABLE #transtmp
      (qty numeric(28,3),wmslocationid nvarchar(20),letter char(1),number int,locationavail nvarchar(20),
      boxweight numeric(28,3),boxes numeric(28))
      
INSERT into #transtmp (qty,wmslocationid,letter,number,locationavail,boxweight,boxes)
values
(1940.000,'FLOOR-A-10','A',10,'FLOOR-A-10',1940.000,1),
(5820.000,'FLOOR-B-17','B',17,'FLOOR-B-17',1940.000,3),
(1940.000,'FLOOR-C-7','C',7,'FLOOR-C-7',1940.000,1),
(3880.000,'FLOOR-B-17','B',17,'FLOOR-B-17',1940.000,2)

Current Result:
qty                  wmslocationid   letter  number    location avail      box weight    boxes
1940.000      FLOOR-A-10            A      10                 FLOOR-A-10      1940.000      1
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3
1940.000      FLOOR-C-7            C      7                 FLOOR-C-7              1940.000      1
3880.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      2

I need help in looping through the result and insert the number of rows from the boxes result.
Expected Result:
qty                  wmslocationid   letter  number    location avail      box weight    boxes   counter
1940.000      FLOOR-A-10            A      10                 FLOOR-A-10      1940.000      1              1
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3              1
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3              2
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3              3
1940.000      FLOOR-C-7            C      7                 FLOOR-C-7              1940.000      1              1
3880.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      2              1
3880.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      2              2
0
Comment
Question by:Kristie
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40312129
May I ask what's the logic for that?
I mean, what for you need to replicate rows?
0
 

Author Comment

by:Kristie
ID: 40312369
Victor, I am replicating row with different counter so I can use that to block off inventory space in an SSRS report.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40312412
This is a perfect use for a tally / sequential numbers table.

Code below will generate 1M numbers (that can be increased if somehow that's needed), then use that to expand the boxes.

;WITH
cteTally10 AS (
    SELECT 0 AS tally UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
),
cteTally100 AS (
    SELECT 1 AS tally
    FROM cteTally10 c1
    CROSS JOIN cteTally10 c2
),
cteTally10K AS (
    SELECT 1 AS tally
    FROM cteTally100 c1
    CROSS JOIN cteTally100 c2
),
cteTally1Mil AS (
    SELECT ROW_NUMBER() OVER(ORDER BY c1.tally) AS tally
    FROM cteTally100 c1
    CROSS JOIN cteTally10K c2
)
SELECT tran.qty, tran.wmslocationid, tran.letter, tran.number, tran.location, tran.avail, tran.[box weight], tran.boxes, t.tally AS counter
FROM #transtmp tran
INNER JOIN cteTally1Mil t ON
    t.tally BETWEEN 1 AND tran.boxes
0
 

Author Comment

by:Kristie
ID: 40312696
Works perfect, Thanks Scott.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up 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.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

810 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