Inserting multiple rows into a table

  • I'm trying to insert multiple rows using the T-SQL below. Is there a more efficient way to do this? I've read a couple posts on here about using the UNION ALL statement.

    USE Test

    GO

    INSERT INTO Practice

    VALUES ('John', 'Jones', '99980', '45', 'Payson', 'Arizona')

    INSERT INTO Practice

    VALUES ('Mary','Jones','99982','25','Payson','Arizona')

    INSERT INTO Practice

    VALUES ('Eric','Edwards','88232','32','San Diego','California')

  • rshafer (12/9/2008)


    I'm trying to insert multiple rows using the T-SQL below. Is there a more efficient way to do this? I've read a couple posts on here about using the UNION ALL statement.

    USE Test

    GO

    INSERT INTO Practice

    VALUES ('John', 'Jones', '99980', '45', 'Payson', 'Arizona')

    INSERT INTO Practice

    VALUES ('Mary','Jones','99982','25','Payson','Arizona')

    INSERT INTO Practice

    VALUES ('Eric','Edwards','88232','32','San Diego','California')

    Yes UNION ALL is the way about it. If you can use SQL2008 then row constructors with VALUES use a more compact syntax


    * Noel

  • Unfortunately I am stuck with 2005 for now. Can you please provide an example using the UNION ALL statement?

  • USE Test

    GO

    INSERT INTO Practice

    select 'John', 'Jones', '99980', '45', 'Payson', 'Arizona'

    union all

    select 'Mary','Jones','99982','25','Payson','Arizona'

    union all

    select 'Eric','Edwards','88232','32','San Diego','California'

    Cheers,

    K

  • k man (12/9/2008)


    USE Test

    GO

    INSERT INTO Practice

    select 'John', 'Jones', '99980', '45', 'Payson', 'Arizona'

    union all

    select 'Mary','Jones','99982','25','Payson','Arizona'

    union all

    select 'Eric','Edwards','88232','32','San Diego','California'

    Cheers,

    K

    Thank you! That worked great.

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

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