SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


replicating records w/o using a cursor


replicating records w/o using a cursor

Author
Message
josh.wayne
josh.wayne
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 5
I have a project where I need to create multiple records out of the same data (exact duplicates) based on the value of a field in the query. For example, a customer has 3 items on an order. Each item has 4 qty ordered. I need my query to return a total of 12 records.

This will in turn produce 12 labels via a Crystal Report (yes, I am constrained to Crystal)

My Query is at the bottom, right beneath some sample code for some test data. I need the records to duplicate based on the value of dbo.SOP10200.QUANTITY.

I would like to avoid cursors due to their known inefficiencies, but I don't know of a better way. Thanks for the help!



----sample code-----
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable

CREATE TABLE #mytable (labeldata varchar(20), quantity numeric(5) )

INSERT INTO #mytable (labeldata, quantity)
SELECT 'labeldata1--item1',4 UNION ALL
SELECT 'labeldata1--item2',4 UNION ALL
SELECT 'labeldata1--item2',4 UNION ALL



-----actual query----
SELECT dbo.SOP10200.SOPNUMBE, dbo.SOP10200.ITEMNMBR, dbo.SOP10200.ITEMDESC, dbo.SOP60300.CUSTITEMNMBR, dbo.SOP60300.CUSTITEMDESC,
dbo.SOP60300.CUSTITEMSHORNAME, dbo.SOP60300.CUSTITEMGENERICDESC, dbo.SOP60300.USERDEF1, dbo.SOP60300.USERDEF2, dbo.SOP60300.USRDEF03,
dbo.SOP60300.USRDEF04, dbo.SOP60300.USRDEF05, dbo.SOP10100.CUSTNMBR, dbo.SOP10100.CUSTNAME, dbo.SOP10100.CSTPONBR,
dbo.SOP10100.CNTCPRSN, dbo.SOP10100.ShipToName, dbo.SOP10100.ADDRESS1, dbo.SOP10100.ADDRESS2, dbo.SOP10100.ADDRESS3, dbo.SOP10100.CITY,
dbo.SOP10100.STATE, dbo.SOP10100.ZIPCODE, dbo.SOP10100.COUNTRY, dbo.SOP10100.PHNUMBR1, dbo.SOP10200.QUANTITY
FROM dbo.SOP10200 INNER JOIN
dbo.SOP10100 ON dbo.SOP10200.SOPTYPE = dbo.SOP10100.SOPTYPE AND dbo.SOP10200.SOPNUMBE = dbo.SOP10100.SOPNUMBE LEFT OUTER JOIN
dbo.IV00101 ON dbo.SOP10200.ITEMNMBR = dbo.IV00101.ITEMNMBR LEFT OUTER JOIN
dbo.SOP60300 ON dbo.SOP10200.ITEMNMBR = dbo.SOP60300.ITEMNMBR AND dbo.SOP10100.CUSTNMBR = dbo.SOP60300.CUSTNMBR
Brandie Tarvin
Brandie Tarvin
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20308 Visits: 9118
Josh,

Can you give us DDL code for the other tables too? And it would help to know which of those tables in the query is the one that #mytable is standing in for.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22291 Visits: 19709
Much simpler than you think - join to a tally table on dbo.SOP10200.QUANTITY.
What folks often forget about tally tables is that they are a source of rows as well as numbers.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Brandie Tarvin
Brandie Tarvin
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20308 Visits: 9118
Chris Morris-439714 (12/3/2010)
What folks often forget about tally tables is that they are a source of rows as well as numbers.


I've actually used a Tally Table to do that too, but a conditional needs to be put on the join to keep it from oversizing the rows.

But does Quantity reflect a single unit or the 4 Qty that the OP was talking about? That's why I asked for DDL (should have also mentioned sample data), because doing it wrong can screw up inventory.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22291 Visits: 19709
Brandie Tarvin (12/3/2010)
Chris Morris-439714 (12/3/2010)
What folks often forget about tally tables is that they are a source of rows as well as numbers.


I've actually used a Tally Table to do that too, but a conditional needs to be put on the join to keep it from oversizing the rows.

But does Quantity reflect a single unit or the 4 Qty that the OP was talking about? That's why I asked for DDL (should have also mentioned sample data), because doing it wrong can screw up inventory.


Not sure yet, Brandie. The sample data is a little short. Good point though.

@Josh: are you using a query tool to generate your SQL or are you writing it freehand?

Edit: blonde moment.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search