Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 621525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:53:05+00:00 2026-05-13T18:53:05+00:00

I am looking for a way to write the below procedure without using a

  • 0

I am looking for a way to write the below procedure without using a CURSOR or just to find a better performing query.

CREATE TABLE #OrderTransaction (OrderTransactionId int, ProductId int, Quantity int);
CREATE TABLE #Product (ProductId int, MediaTypeId int);
CREATE TABLE #OrderDelivery (OrderTransactionId int, MediaTypeId int);

INSERT INTO #Product (ProductId, MediaTypeId) VALUES (1,1);
INSERT INTO #Product (ProductId, MediaTypeId) VALUES (2,2);
INSERT INTO #OrderTransaction(OrderTransactionId, ProductId, Quantity) VALUES (1,1,1);
INSERT INTO #OrderTransaction(OrderTransactionId, ProductId, Quantity) VALUES (2,2,6);

DECLARE @OrderTransactionId int, @MediaTypeId int, @Quantity int; 

DECLARE ordertran CURSOR FAST_FORWARD FOR 
    SELECT  OT.OrderTransactionId, P.MediaTypeId, OT.Quantity
    FROM    #OrderTransaction OT WITH (NOLOCK)
        INNER JOIN  #Product P WITH (NOLOCK)
            ON OT.ProductId = P.ProductId


OPEN ordertran;
FETCH NEXT FROM ordertran INTO @OrderTransactionId, @MediaTypeId, @Quantity;

WHILE @@FETCH_STATUS = 0 
BEGIN
    WHILE @Quantity > 0 
    BEGIN
        INSERT INTO #OrderDelivery ([OrderTransactionId], [MediaTypeId])
        VALUES (@OrderTransactionId, @MediaTypeId)

        SELECT @Quantity = @Quantity - 1;
    END 

    FETCH NEXT FROM ordertran INTO @OrderTransactionId, @MediaTypeId, @Quantity;
END 

CLOSE ordertran;
DEALLOCATE ordertran;


SELECT  *   FROM    #OrderTransaction 
SELECT  *   FROM    #Product
SELECT  *   FROM    #OrderDelivery

DROP TABLE #OrderTransaction;
DROP TABLE #Product;
DROP TABLE #OrderDelivery;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:53:05+00:00Added an answer on May 13, 2026 at 6:53 pm

    Begin with a Numbers table that is large enough to handle the maximum order amount:

    CREATE TABLE Numbers (
       Num int NOT NULL PRIMARY KEY CLUSTERED
    )
    
    -- SQL 2000 version
    INSERT Numbers VALUES (1)
    SET NOCOUNT ON
    GO
    INSERT Numbers (Num) SELECT Num + (SELECT Max(Num) FROM Numbers) FROM Numbers
    GO 15
    
    -- SQL 2005 and up version
    WITH
       L0 AS (SELECT c = 1 UNION ALL SELECT 1),
       L1 AS (SELECT c = 1 FROM L0 A, L0 B),
       L2 AS (SELECT c = 1 FROM L1 A, L1 B),
       L3 AS (SELECT c = 1 FROM L2 A, L2 B),
       L4 AS (SELECT c = 1 FROM L3 A, L3 B),
       L5 AS (SELECT c = 1 FROM L4 A, L4 B),
       N AS (SELECT Num = ROW_NUMBER() OVER (ORDER BY c) FROM L5)
    INSERT Numbers(Num)
    SELECT Num FROM N
    WHERE Num <= 32768;
    

    Then, immediately after your INSERT statements:

    INSERT #OrderDelivery (OrderTransactionId, MediaTypeId)
    SELECT
       OT.OrderTransactionId,
       P.MediaTypeId
    FROM
       #OrderTransaction OT
       INNER JOIN #Product P ON OT.ProductId = P.ProductId
       INNER JOIN Numbers N ON N.Num BETWEEN 1 AND OT.Quantity
    

    That should do it!

    If for some reason you have qualms about putting a permanent Numbers table in your database (which I don’t understand as it is a wonderful tool), then you can simply join to the CTE given instead of the table itself. In SQL 2000 you can create a temp table and use a loop, but I would advise against this strongly.

    A Numbers table is highly recommended. There is no concern about some future change breaking it (the set of whole numbers won’t change any time soon). Some people use a Numbers table with a million numbers in it, which is only around 4MB of storage.

    To answer critics of the Numbers table: if the database design uses a numbers table, then that table won’t need to change. It is like any other table in the database and can be relied on. You don’t worry too much about queries against an Orders table failing because some day the table might not exist, so I don’t see why there would be any similar concern about another table that is required and depended on.

    UPDATE

    In the time since writing this answer I have learned about the master.dbo.spt_values table which has a number column. When queried with where type='P' you get 0 – 255 in SQL 2000 and 0 – 8191 in SQL 2005 and up. (There are also potentially useful low and high columns.) You can cross join this table to itself a couple of times if necessary to get, even in SQL 2000, a bunch of rows very quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to write a search and replace method using W3C
I'm looking for way to write Javascript programs / scripts on desktop, not inside
What I'm looking for is a way to write out a python Fraction() type
I'm looking for way to create list view as like in IOS with pinned
I am using codeigniter and looking a way to enable directly editting of doc
I'm looking to create a free-type textbox which onkeyout filters a datagrid below. The
I've just started coding in Perl and am just looking to find out if
I am looking for a way, using static analysis of two JavaScript functions, to
I'm looking for a way to write xyz in FireFox, and when I press
I'm looking a way to enable IP logging with log4net in ASP.NET. I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.