SQL Server Insert stored procedure result into specific columns in table [duplicate]

xtfmy6hx  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(98)

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)
wqsoz72f

wqsoz72f1#

Do it like this

INSERT INTO people
(run, first_name, last_name, dob, address, insert_date, processed_flag)
SELECT 1, first_name, last_name, dob, address ,'2023-07-19', 2
FROM #peopletmp

There are several ways to split it into 8 different processed flags depending on your rules. Here are two examples you could adapt

-- generate some demo data
declare @demo table (dname varchar(255), dtype varchar(255));
insert into @demo (dname, dtype) select [name],[type] from sys.objects;

-- divide into 8 groups 1, 2, 3, 4, 5, 6, 7, 0 based on the order of one of the data items
select dname, (ROW_NUMBER() over (order by dname)) % 8 as groupNumber
from @demo;

alternatively, and probably the more likely scenario...

-- generate some more demo data 
declare @types table (groupNumber int identity(1,1), dtype varchar(255));
insert into @types (dtype) select distinct [type] from sys.objects;

-- divide into 8 groups 1 through 7 based on some arbitrary rules
select d.dname, d.dtype, t.groupNumber
from @demo d
inner join @types t on d.dtype = t.dtype;

Finally, another technique based on first letter of a name

-- Generate some more demo data based on alphabet
declare @alphas table (alpha char(1), groupNumber int);
insert into @alphas (alpha)
select top 26 CHAR((ROW_NUMBER() over (order by name)) + 64) 
from sys.objects;
-- Break the alphabet into 8 arbitrary groups
update @alphas 
set groupNumber = CASE 
        WHEN alpha IN ('A','B','C') THEN 1
        WHEN alpha IN ('D','E','F') THEN 2
        WHEN alpha IN ('G','H','I','J') THEN 3
        WHEN alpha IN ('K','L','M') THEN 4
        WHEN alpha IN ('N','O','P') THEN 5
        WHEN alpha IN ('Q','R','S') THEN 6
        WHEN alpha IN ('T','U','V','W') THEN 7
        WHEN alpha IN ('X','Y','Z') THEN 8
    END;

-- Group into 8 categories based on 1st letter of name
select d.dname, d.dtype, a.groupNumber
from @demo d
join @alphas a on a.alpha = LEFT(d.dname,1);

相关问题