user2575061
user2575061

Reputation:

How to use select into statement?

I want to insert records from Table1 and Table2 into Table3 and my Table3 has Two columns:

studentId
subjectId

And I want to insert these 2 values from Table1(contains 1000 student Id's) and From Table2(contains 5 subjects). To achieve that I have used following query but it gave me error

Query:

INSERT INTO StudentSubject(studentId,subjectId)
SELECT studentId FROM Table1 UNION SELECT subjectId FROM Table2

But I got this error message:

Msg 120, Level 15, State 1, Line 1 The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.

Upvotes: 1

Views: 67

Answers (1)

Bogdan Sahlean
Bogdan Sahlean

Reputation: 1

INSERT into StudentSubject(studentId,subjectId)
SELECT a.studentId,b.subjectId 
FROM Table1 a CROSS JOIN Table2 b

Upvotes: 1

Related Questions