rubinski_be
rubinski_be

Reputation: 11

Insert Query with Subquery

I'm trying to make an Insert query with a subquery. I have to insert other data apart from the subquery result. This is the query I have right now:

INSERT INTO articles (title,content,frontpage,date_created,userID,catID,sectionID) 
values("merijnmoetleren","blalblrsklfdkf", 1, "2010-01-23", 5, 2,
(SELECT id FROM sections WHERE name ="about")

What's wrong with it?

Upvotes: 1

Views: 1009

Answers (4)

THEn
THEn

Reputation: 1938

INSERT INTO articles ( title
                     , content
                     , frontpage
                     , date_created
                     , userID
                     , catID
                     , sectionID
                     ) 
              values ( "merijnmoetleren"
                     , "blalblrsklfdkf"
                     , 1
                     , "2010-01-23"
                     , 5
                     , 2
                     , (SELECT TOP 1 id FROM sections WHERE name ="about")
                     )

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53543

Try this:

INSERT INTO articles
  (title, content, frontpage, date_created, userID, catID, sectionID)
  SELECT "merijnmoetleren", "blalblrsklfdkf", 1, "2010-01-23", 5, 2, id
  FROM sections WHERE name = "about";

Upvotes: 5

Nick Rolando
Nick Rolando

Reputation: 26167

See if this works

INSERT INTO articles ( title, content, frontpage, date_created, userID, catID, sectionID ) SELECT "merijnmoetleren","blalblrsklfdkf", 1, "2010-01-23", 5, 2, id FROM sections WHERE name ="about"

Upvotes: 5

carlbenson
carlbenson

Reputation: 3207

Put another closing paranthesis at the end.

Upvotes: 2

Related Questions