Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

replicating records w/o using a cursor Expand / Collapse
Author
Message
Posted Monday, November 29, 2010 2:35 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 20, 2011 1:12 PM
Points: 1, 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
Post #1027623
Posted Friday, December 3, 2010 5:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:34 AM
Points: 7,132, Visits: 6,293
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1029789
Posted Friday, December 3, 2010 6:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 7,128, Visits: 13,509
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
Post #1029828
Posted Friday, December 3, 2010 6:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:34 AM
Points: 7,132, Visits: 6,293
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1029837
Posted Friday, December 3, 2010 6:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 7,128, Visits: 13,509
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
Post #1029838
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse