Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Posts
    20

    Unanswered: SQL help? INSERT INTO with subquery...

    Hi!

    I need a bit of help with my code. I'm trying to insert some specific value as well as some wirtable values into my table, but I get an error: "Subquery cannot appear in an Insert Values statement."

    Example:
    I want to fill out the Name, Date and Number. Also I want to fill automaticly the Id_number from another_table where a Opened (boolean) value is chosen as "True".
    So, I came up with the following query...



    INSERT INTO dbo.Persons (Name, Date, Number, Opened)
    VALUES (@Name, @Date, @Number, (SELECT Id_number FROM dbo.another_table WHERE Opened = 'True'))






    So, how can I get the ID info from another table that responds to the requirement of Opened=True and insertl it in with the other info I fill in?

  2. #2
    Join Date
    Sep 2001
    Location
    Chicago, Illinois, USA
    Posts
    601
    Is this what you are trying to do?

    Code:
    INSERT
    INTO    dbo.Persons
            (
            Name
            ,Date
            ,Number
            ,Opened
            )
    SELECT  @Name Name
            ,@Date Date
            ,@Number Number
            ,Id_number Opened
    FROM    dbo.another_table
    WHERE   Opened = 'True'
    Ken

    Maverick Software Design

    (847) 864-3600 x2

  3. #3
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by PracticalProgram View Post
    Is this what you are trying to do?

    Code:
    INSERT
    INTO    dbo.Persons
            (
            Name
            ,Date
            ,Number
            ,Opened
            )
    SELECT  @Name Name
            ,@Date Date
            ,@Number Number
            ,Id_number Opened
    FROM    dbo.another_table
    WHERE   Opened = 'True'


    Yup, but the @-values were supposed to be user defined/ user added values.
    I actually noticed that this problem accurend while working with SQL in Visual Studio. Figured out now how to do it... Thanks for your interest anyway!

    (I added a disabled textbox that imported the ID number of the row that has "Opened" as True. Then just insert -action that inserts all the @-values that the user writes + the Id_number - textbox and VOILÁ...)

Tags for this Thread

Posting Permissions

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