Carlos Mendieta
Carlos Mendieta

Reputation: 872

Incorrect syntax near the keyword 'SELECT'. Incorrect syntax near ')' when attempting using an inner select

I've seen the error before for a different reason but how can I fix this?

INSERT INTO [dbo].[XXX] ([ID], [aaa], [bbb], [ccc])
VALUES (SELECT NEXT VALUE FOR dbo.S_SHIPPING_ID_SEQ, @aaa, @bbb, @ccc)

Thank you for the help

Upvotes: 0

Views: 132

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

Just use insert . . . select. No values is needed:

INSERT INTO [dbo].[XXX] ([ID], [aaa], [bbb], [ccc])
    SELECT NEXT VALUE FOR dbo.S_SHIPPING_ID_SEQ, @aaa, @bbb, @ccc;

Or, if you really like VALUES, use a subquery:

INSERT INTO [dbo].[XXX] ([ID], [aaa], [bbb], [ccc])
    VALUES (SELECT NEXT VALUE FOR dbo.S_SHIPPING_ID_SEQ), @aaa, @bbb, @ccc);

And, actually, this should also work:

INSERT INTO [dbo].[XXX] ([ID], [aaa], [bbb], [ccc])
    VALUES (NEXT VALUE FOR dbo.S_SHIPPING_ID_SEQ, @aaa, @bbb, @ccc);

But why not just declare ID to be an identity and use:

INSERT INTO [dbo].[XXX] ([aaa], [bbb], [ccc])
    VALUES (@aaa, @bbb, @ccc);

Upvotes: 3

Related Questions