?
Solved

Row Insert

Posted on 2014-09-09
4
Medium Priority
?
185 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
[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
4 Comments
 
LVL 52

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

719 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