This question already has answers here:
Insert into ... values ( SELECT ... FROM ... ) (27 answers)
Closed 13 days ago.
I have a stored procedure that generates a list of people. It outputs 4 columns:
first_name
last_name
dob
address
I execute my stored procedure, and put it in a temp table named peopletmp
. The issue I am running in to is when I am trying to insert info, along with other things, from my temp table in to my primary people
table I am getting a couple of different errors:
Msg 116, Level 16, State 1, Line 14
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 109, Level 15, State 1, Line 12
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
Can I not insert into my people table with the subquery and other information at the same time? Below is my insert statement:
INSERT INTO people (run, first_name, last_name, dob, address, insert_date, processed_flag)
VALUES (1, (SELECT first_name, last_name, dob, address FROM #peopletmp), '2023-07-19', 2)
1条答案
按热度按时间wqsoz72f1#
Do it like this
There are several ways to split it into 8 different processed flags depending on your rules. Here are two examples you could adapt
alternatively, and probably the more likely scenario...
Finally, another technique based on first letter of a name