Insert multiple of 50 records into tables of headers and rows.

  • Hello,I would like to insert from a table of articles with (1113 records), in a table (a) headers and (b) lines, insertions of 50 rows in 50 rows, for each header.That is, in table (a) the record a1 has 50 records in table (b1) of the article table, until all 1113 records have been posted.

  • performance wise, SQL server can insert millions of rows in about the same time as a single row or small number of rows,
    so breaking up an operation into multiple steps would be that many steps slower(in your example, 22 times or so) ,
    so the first question everyone will have is why? there's no reason to break it up, especially with a small amount of records.

    regardless, to do something like this you need a loop of some kind. a cursor or while loop can do the iteration you are asking.
    The more detail you provide, the better example we can give you.
    It SEEMS like you are asking how to insert data into TWO tables, like a header/detail, right?
    for that, if you need a reference to the header table, you need the output clause, or to re-join the header to the insert based on the description inserted.
    the typical way to do it all at once in a pair of statements

    CREATE TABLE Articles(
    Articleid INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Title VARCHAR(100) )
    INSERT INTO Articles(Title)
    SELECT Header FROM SomeOtherTable

    INSERT INTO ArticleDetails(Articleid ,ArticleBody)
    SELECT at.ArticleID,ot.Detail FROM SomeOtherTable ot
    INNER JOIN Articles at ON ot.Header = at.Title

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • That's right, but i want to insert only 50 lines for each header until use all articles without repeat any. Example header n°1 - articles 1to50 ,
    Header n°2 - articles 51to101, and so on. 
    Will be a set of stock documents in which each document only has 50 lines of articles.

  • we need Details. the actual DDL of your current table, a couple of fake sample rows of data, and the DDL of your destination tables.
    with that, we can provide a tested, verified example.

    aside from that, maybe what you are asking is how you can make your "virtual" header in groups of fifty.

    you can do that with the NTILE function, which will group your data into groups of a max size of fifty in this example...is that what you want?

    SELECT NTILE(50) OVER(ORDER BY name)  AS HeaderID,
    name FROM sys.all_objects

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • juliosousa90 - Friday, June 23, 2017 6:21 AM

    That's right, but i want to insert only 50 lines for each header until use all articles without repeat any. Example header n°1 - articles 1to50 ,
    Header n°2 - articles 51to101, and so on. 
    Will be a set of stock documents in which each document only has 50 lines of articles.

    Please provide some sample data, showing its desired state, before and after.
    Also, you have not actually asked for anything – instead, you have described what you want to achieve. What is your question?
    And please also describe the logic behind why it is necessary to use increments of 50. Given what you have told us, this requirement seems arbitrary and unnecessary.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • TBL Articles
    IdNameUnitPriceIdwarehouse
    1   arcticle01$11
    2   arcticle02$12
    3   arcticle03$13
    4   arcticle04$14
    5   arcticle05$15
    totototo
    1113   article1113$11113

    Now I want to automatically insert through the slq the data in the tables below
    50 articles in each CDStockRows

    TBL CDStock (Id, NumberDoc, NameDocument, date, Total)

    TBL CDStockRows (Id, IdCDStock, RowNumber, IdArticle, AmountUnit, Price, TotalRow)
  • TBL CDStock 
    IdNumberDocDateTotal
    1123-06-2017$50

    TBL CDStockRows
    IdIdCDStockRowNumberIdArticleAmountUnitPriceTotalRow
    111A11$1$1
    212A24$1$4
    313A32$2$2
    50150A503$1$3

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply