0
INSERT into LibraryTable(ID, Artist, Name, Rating, PlayCount) VALUES(
Select MIN(a.ID + 1)
From LibraryTable A
Left Join LibraryTable B
On A.ID = B.ID - 1
Where B.ID Is NULL, 'eg', 'eg', 1, 1)

im getting a syntax error at "Select" and the "," after null. im a beginner so any helpful tips would be greatly appreciated.

2
  • VALUES does not work with SELECT in that way, just do INSERT INTO table(field, ...) SELECT... Commented Mar 2, 2014 at 13:41
  • You are trying to insert a single value (MIN(a.ID+1)) to multiple columns (ID, Artist, Name, Rating, PlatCount) Commented Mar 2, 2014 at 13:42

3 Answers 3

3

You want insert . . . select:

INSERT into LibraryTable(ID, Artist, Name, Rating, PlayCount)
    Select MIN(a.ID + 1), 'eq', 'eq', 1, 1
    From LibraryTable A Left Join
         LibraryTable B
         On A.ID = B.ID - 1
    Where B.ID Is NULL;

The values keyword is not used for this form of the insert.

If you are using SQL Server 2012, you can do this with the lead() function instead of a self join:

INSERT into LibraryTable(ID, Artist, Name, Rating, PlayCount)
    Select MIN(lt.ID + 1), 'eq', 'eq', 1, 1
    From (select lt.*, lead(id, 1) over (order by id) as nextid
          from LibraryTable lt
         ) lt Left Join
    where nextid <> id + 1;
Sign up to request clarification or add additional context in comments.

1 Comment

superb, thanks for everyone's help, they all sorta worked but i can only choose one, again thanks everyone.
0

TRY this:

INSERT into LibraryTable(ID, Artist, Name, Rating, PlayCount) 
Select MIN(a.ID + 1) as ID, 'eg' as Artist, 'eg' as Name, 1 as Rating, 1 as PlayCount
From LibraryTable A
Left Join LibraryTable B
On A.ID = B.ID - 1
Where B.ID Is NULL

Comments

0

You are trying to insert one value, but the syntax expects five. Try...

INSERT  INTO LibraryTable
        ( ID ,
          Artist ,
          Name ,
          Rating ,
          PlayCount
        )
        SELECT  MIN(a.ID + 1) AS ID ,
                'eg' ,
                'eg' ,
                1 ,
                1
        FROM    LibraryTable A
                LEFT JOIN LibraryTable B ON A.ID = B.ID - 1
        WHERE   B.ID IS NULL

There's an article here that explains.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.