根据条件将值插入表中,sql server [英] Insert values into table based on condition, sql server
问题描述
我尝试根据条件在存储过程中将值插入到我的表中:
I try to insert values into my table in stored procedure based on the conditions:
alter procedure service_report_st2
as
insert into Service_report_step1(appointment_or_house)
select
case
when Service_report_step1.card_uid in(select card_uid from visits where vidpos_kod = 'A')
then '1'
when Service_report_step1.card_uid in(select card_uid from visits where vidpos_kod = 'B')
then '2'
else '3'
end
错误在 Service_report_step1.card_uid
中未找到,但这是 Service_report_step1
表中的第一列.appointment_or_house
是第二列,如果 visits
表在 中包含
.card_uid
,则应包含1"、2"或3"Service_report_step1
The error is in Service_report_step1.card_uid
it is not found, however this is the first column in Service_report_step1
table. appointment_or_house
is the second column, which should contain '1', '2' or '3' if visits
table contains card_uid
in Service_report_step1
.
非常感谢您的帮助!
简短示例:
Visits
表有 2 列:Card_uid 和 vidpos_kod
Visits
table has 2 columns: Card_uid and vidpos_kod
card_uid vidpos_kod
111-111 A
222-222 B
333-333 A
444-444 C
现在Service_report_step1
表有card_uid(包含所有可能的卡片的列)和appointment_or_house列
now Service_report_step1
table has card_uid (column that contains all possible cards) and appointment_or_house column
card_uid appointment_or_house
111-111 1
222-222 2
333-333 1
444-444 1
因此,如果 Service_report_step1
中的 card_uids
与 Visits
中的 card_uids
相同,我会根据以下内容确定 appointment_or_house
列Visits
中的vidpos_kod
(A,C
为'1',B
为'2')
So, If I have the same card_uids
in Service_report_step1
as in Visits
I determine appointment_or_house
column based on vidpos_kod
in Visits
(A, C
is '1', B
is '2')
我需要像示例一样将所有数据正确插入 Service_report_step1
.
I need to insert into Service_report_step1
all data correctly as in example.
推荐答案
insert into Service_report_step1(appointment_or_house)
select
case vidpos_kod
when 'A' then '1'
when 'B' then '2'
when 'C' then '3'
end
from visits
where vidpos_kod like '[ABC]'
这篇关于根据条件将值插入表中,sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!