Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Posts
    12

    Unanswered: Select form 1 table and insert into another

    Can someone point me in the right direction or show me a sample that may assist me in the following.

    Thanks,
    -dw


    I need to select some data from 1 table and insert it into another using a stored procedure.

    I also would like to select data from table 1 and insert a new record into table 1 modifying a field with new data passed by parameter.

    Is it something like:

    parameters passed = @selectJobNumber, @newJobNumber


    declare @DeliveryMethodID int

    SELECT
    @DeliveryMethodID=DeliveryMethodID,
    ... etc - more fields

    FROM
    jobDeliveryAddress

    WHERE
    (JobNumber= @selectJobNumber)


    INSERT INTO [jobDeliveryAddress]
    (
    [JobNumber],
    [DeliveryMethodID],
    ... etc - more fields

    )
    VALUES
    (
    @newJobNumber,
    @DeliveryMethodID,
    ... etc

    )
    -dw

  2. #2
    Join Date
    Sep 2002
    Location
    Mancos, CO
    Posts
    73

    Re: Select form 1 table and insert into another

    Originally posted by liffey
    Can someone point me in the right direction or show me a sample that may assist me in the following.

    Thanks,
    -dw


    I need to select some data from 1 table and insert it into another using a stored procedure.

    I also would like to select data from table 1 and insert a new record into table 1 modifying a field with new data passed by parameter.

    Is it something like:

    parameters passed = @selectJobNumber, @newJobNumber


    declare @DeliveryMethodID int

    SELECT
    @DeliveryMethodID=DeliveryMethodID,
    ... etc - more fields

    FROM
    jobDeliveryAddress

    WHERE
    (JobNumber= @selectJobNumber)


    INSERT INTO [jobDeliveryAddress]
    (
    [JobNumber],
    [DeliveryMethodID],
    ... etc - more fields

    )
    VALUES
    (
    @newJobNumber,
    @DeliveryMethodID,
    ... etc

    )
    First question (inserting from table1 to table2) would be the following:

    insert into table2 (field1, field2, field3)
    Select field1, field2, 'USA' as field3
    from table2
    where jobnumber = @selectjobnumber

    The same format should work for your second question although the example isn't clear on what is different with the record being inserted.

  3. #3
    Join Date
    Jan 2004
    Posts
    12

    Re: Select form 1 table and insert into another

    Thanks, baolive, for the reply. For some reason my post seems to have appeard on the board twice!

    This instance is the second.

    I got a reply on the first instance along the lines of your reply. This solved my difficulty.

    Thanks and sorry for the confusion.
    -dw

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •