Insert INTO table from select query in SQL Server 2005

Insert INTO table from select query in SQL Server 2005

The INSERT INTO statement is used to insert new records in a table. But in SQL Server 2005 we cannot write an insert into statement & insert the whole tables or output values into a table. We have some other approaches out of which I'll showcase 3 of them in my post as mentioned below:

1st Approach:

INSERT INTO [MyDB].[dbo].[MyTable]
      ([FieldID]
      ,[Description])
VALUES
      (1000,N'test')

2nd Approach:

INSERT INTO [MyDB].[dbo].[MyTable]
       ([FieldID]
       ,[Description])
VALUES
       (1001,N'test2')
One other option is to use UNION ALL:
INSERT INTO [MyDB].[dbo].[MyTable]
      ([FieldID]
       ,[Description])
SELECT 1000, N'test' UNION ALL
SELECT 1001, N'test2'

3rd Approach:

INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO



You may also like

No comments:

If you have any queries regarding the article or in performing something in Microsoft SQL Server then please let me know.