Thursday, 7 May 2015

Insert Multiple Records Using One Insert Statement

Insert Multiple Records Using One Insert Statement

Creating table:
CREATE TABLE [dbo].[authors](
      [au_id] [int] NOT NULL,
      [Au_fname] [varchar](50) NULL)
Old Method 1:
INSERT INTO authors  (Au_fname,au_id) VALUES ('First',11);
INSERT INTO authors  (Au_fname,au_id) VALUES ('Second',12);
INSERT INTO authors  (Au_fname,au_id) VALUES ('Third',13);
INSERT INTO authors  (Au_fname,au_id) VALUES ('Fourth',14);
INSERT INTO authors  (Au_fname,au_id) VALUES ('Fifth',15);
Next Method 2:
INSERT INTO authors  (Au_fname,au_id)
    SELECT  'First' ,11
    UNION ALL
SELECT  'Second' ,12
    UNION ALL
SELECT  'Third' ,13
    UNION ALL
SELECT  'Fourth' ,14
    UNION ALL
SELECT  'Fifth' ,15
GO

New Method 3:
INSERT INTO authors  (Au_fname,au_id)
    VALUES ('First',11),
           ('Second',12),
           ('Third',13),
           ('Fourth',14),

            ('Fifth',15)

No comments:

Post a Comment