Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Insert statement with multiple rows

Tags:

sql

sql-server

This seems really trivial, but for the life of me I cannot understand why this is not working. Any ideas would be great!

USE [ACCTQA]
GO

INSERT INTO [dbo].[glacct]
           ([Company]
           ,[Account]
           ,[Description])
     VALUES
 ('E096','191802','INVESTMENT IN SUBS')
,('E096','320007','COMMON STOCK')
,('E096','330013','SURPLUS')
,('E096','340003','RETAINED EARNINGS')
,('E096','690876','LIAB & CASUALTY INS')
,('E096','710000','BONDS - AGENCY BILL')
,('E096','710001','COMM. P&C - AGENCY BILL')
,('E096','710002','EMPL BENEFITS-AGENCY BILL')

ERROR MSG:

Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ','.
like image 641
AG2275 Avatar asked Jun 22 '26 11:06

AG2275


1 Answers

Try this

USE [ACCTQA]
GO

INSERT INTO [dbo].[glacct]
           ([Company]
           ,[Account]
           ,[Description])
SELECT * FROM (   
SELECT 'E096','191802','INVESTMENT IN SUBS' UNION ALL
SELECT 'E096','320007','COMMON STOCK' UNION ALL
SELECT 'E096','330013','SURPLUS' UNION ALL
SELECT 'E096','340003','RETAINED EARNINGS'
.....)

I'm guessing your version is not accepting multiple values like this, so you have to use a select statement(INSERT FROM SELECT) to make it work.

like image 117
sagi Avatar answered Jun 24 '26 00:06

sagi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!