Results 1 to 6 of 6

Thread: [RESOLVED] INSERT with Multiple Queries

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Resolved [RESOLVED] INSERT with Multiple Queries

    Hi Guys,

    I don't know how to write this in proper SQL Commands:

    Code:
    INSERT INTO tblTable1
    (
       Field1,
       Field2,
       Field3
    )
    VALUES
    (
       @Field1,
       (SELECT FieldID FROM tblTable2 WHERE FieldName = @FieldName),
       (SELECT FieldID FROM tblTable3 WHERE FieldName = @FieldName)
    )
    That code doesn't work with my Stored Procedure.

    Thanks in advance

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: INSERT with Multiple Queries

    Why not something like this?
    Code:
        DECLARE @value1 nvarchar(200)
        DECLARE @value2 nvarchar(200)
        SET @value1 = SELECT FieldID FROM tblTable2 WHERE FieldName = @FieldName
        SET @value2 = SELECT FieldID FROM tblTable3 WHERE FieldName = @FieldName
        INSERT INTO tblTable1
        (
           Field1,
           Field2,
           Field3
        )
        Values
        (
           @Field1,
           @value1,
           @value2)
        )
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: INSERT with Multiple Queries

    when I tried to use this code:

    Code:
    SET @varNames = SELECT NameID FROM tblNames WHERE Name = @Name

    This message appears at the bottom.


    Code:
    Msg 156, Level 15, State 1, Procedure prPODataEntry, Line 47
    Incorrect syntax near the keyword 'SELECT'.

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: INSERT with Multiple Queries

    change it to -

    Code:
    SELECT @varNames = NameID FROM tblNames WHERE Name = @Name
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: INSERT with Multiple Queries

    Thanks guys,
    and also I have learned that we can also write it like this:

    Code:
    SET @varNames = (SELECT NameID FROM tblNames WHERE Name = @Name)

  6. #6
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: INSERT with Multiple Queries

    or if u want to insert multiple value to single field, u can use something like this
    Code:
    INSERT INTO tblTable1
    (
       Field1
    )
       SELECT @Field1
       UNION
       SELECT FieldID FROM tblTable2 WHERE FieldName = @FieldName
       UNION
       SELECT FieldID FROM tblTable3 WHERE FieldName = @FieldName

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

Posting Permissions

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



Click Here to Expand Forum to Full Width