Jamesla
Jamesla

Reputation: 1408

Having trouble with insert into syntax

I am new to sql.

I have a table with 2 columns called Question And ID named Test.

I am trying to take 20 random questions from a table called questions and insert them into the tests table with the same ID for each query. That means there should be 20 rows in the tests table after this query is executed all with the same ID.

The pseudo code would look something like this.

I have hard coded @id for this example.

Declare @id int = 5
Insert into Test ((select top 20 questions from questions orderby newid()), @id))

Can somebody please help me with the syntax to achieve this? It would be much appreciated.

Upvotes: 2

Views: 40

Answers (1)

John Woo
John Woo

Reputation: 263693

convert it into INSERT INTO..SELECT syntax

DECLARE @id = 5
INSERT INTO Test (Question, ID)
SELECT TOP 20 questions, @id
FROM   questions 
ORDER  BY NEWID()

Upvotes: 4

Related Questions