Need help with INSERT statement with sub-query

  • Hi,

    The following insert statement was not compiled.

           INSERT INTO my_table (comments,

                                           sr_status,

                                           sr_notes_count,

                                           sr_activity_count,

                                           last_updated_dt)

            VALUES ('some input comments',

                        (SELECT status FROM my_table_2 WHERE id = 123)

                        (SELECT count(*) FROM my_table_3 WHERE id = 123)

                        (SELECT count(*) FROM my_table_4 WHERE id = 123) 

                        GETDATE())

    Is there a way to use sub-query within an INSERT statement?

    THanks,

    Tuan

  • subqueries can be used in values clause of insert statement, but it should return only single value

     

     

  • all my sub-queries return a single value. But my statement didn't compile. How do I correct the above statement?

    THanks,

    Tuan

  • try:

     INSERT INTO my_table (comments,

                                    sr_status,

                                    sr_notes_count,

                                    sr_activity_count,

                                    last_updated_dt)

     SELECT 'some input comments',

                 (SELECT max(status) FROM my_table_2 WHERE id = 123),

                 (SELECT count(*) FROM my_table_3 WHERE id = 123),

                 (SELECT count(*) FROM my_table_3 WHERE id = 123),

                 GETDATE()

    You may also need to account for nulls

  • I guess it will work , do not use values clause

    try:

     INSERT INTO my_table (comments,

                                    sr_status,

                                    sr_notes_count,

                                    sr_activity_count,

                                    last_updated_dt)

     SELECT 'some input comments',

                 (SELECT status FROM my_table_2 WHERE id = 123),

                 (SELECT count(*) FROM my_table_3 WHERE id = 123),

                 (SELECT count(*) FROM my_table_3 WHERE id = 123),

                 GETDATE()

     

  • Not really sure but I think it is a simple case of missing comma's.  Compare this insert to the original and let us know.

         INSERT INTO my_table (comments,

                                           sr_status,

                                           sr_notes_count,

                                           sr_activity_count,

                                           last_updated_dt)

            VALUES ('some input comments',

                        (SELECT status FROM my_table_2 WHERE id = 123),

                        (SELECT count(*) FROM my_table_3 WHERE id = 123),

                        (SELECT count(*) FROM my_table_4 WHERE id = 123),

                        GETDATE())

    hth,

    Lynn

     

  • You don't need subquery at all.

    For better performance always try to build "one go" query.

    INSERT INTO my_table (comments,

    sr_status,

    sr_notes_count,

    sr_activity_count,

    last_updated_dt)

    SELECT 'some input comments', T2.status,

    count(T3.id), count(T4.id), GETDATE()

    FROM my_table_2 T2

    INNER JOIN my_table_3 T3 ON T2.id = T3.id

    INNER JOIN my_table_4 T4 ON T2.id = T4.id

    GROUP BY T2.Id, T2.status

    WHERE T2.id = 123

    And better forget about VALUES clause. Forever.

    _____________
    Code for TallyGenerator

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

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