我想在sproc中使用插入查询中的IF-Else。我尝试过这段代码,并收到错误。 怎么了?
create procedure sp_m_ii_2_kaydet
@mtu int,
@mod int,
@moid int,
@uid int
as
insert into m_ii2_alisfatura
(muhtur, mturid, muhodm, modmid, uid) values
(@mtu,
IF (@mtu = 3) SELECT MAX(id) FROM m_ii1_vsf_personel
ELSE IF (@mtu = 2) SELECT MAX(id) FROM m_ii1_vsf_firma
ELSE IF (@mtu = 4) SELECT MAX(id) FROM m_ii1_vsf_taseron,
@mod, @moid, @uid)
答案 0 :(得分:1)
这不是一个有效的语法试试这个
您需要使用三种不同的插入语句
var dataTest = data.replace(/[^-0-9]/gi, '');
或通过过滤where子句
中的数据来使用CREATE PROCEDURE Sp_m_ii_2_kaydet @mtu INT,
@mod INT,
@moid INT,
@uid INT
AS
IF ( @mtu = 3 )
INSERT INTO m_ii2_alisfatura
(muhtur,mturid,muhodm,modmid,uid)
SELECT @mtu,Max(id),@mod,@moid,@uid
FROM m_ii1_vsf_personel
ELSE IF ( @mtu = 2 )
INSERT INTO m_ii2_alisfatura
(muhtur,mturid,muhodm,modmid,uid)
SELECT @mtu,Max(id),@mod,@moid,@uid
FROM m_ii1_vsf_firma
ELSE IF ( @mtu = 4 )
INSERT INTO m_ii2_alisfatura
(muhtur,mturid,muhodm,modmid,uid)
SELECT @mtu,Max(id),@mod,@moid,@uid
FROM m_ii1_vsf_taseron
union all
甚至CREATE PROCEDURE Sp_m_ii_2_kaydet @mtu INT,
@mod INT,
@moid INT,
@uid INT
AS
INSERT INTO m_ii2_alisfatura
(muhtur,mturid,muhodm,modmid,uid)
SELECT @mtu,Max(id),@mod,@moid,@uid
FROM m_ii1_vsf_personel
WHERE @mtu = 3
UNION ALL
SELECT @mtu,Max(id),@mod,@moid,@uid
FROM m_ii1_vsf_firma
WHERE @mtu = 2
UNION ALL
SELECT @mtu,Max(id),@mod,@moid,@uid
FROM m_ii1_vsf_taseron
WHERE @mtu = 4
声明
case
注意:如果至少有一个条件不匹配,前两个查询将不会插入任何记录。但case语句将插入数据将为null值,即使没有条件匹配
答案 1 :(得分:0)
infact 您可以使用类似的语法。
<强>例如强>
create table #tt(Id int)
create table #tu(tid int)
insert into #tu values(1),(2)
declare @test int = 1
insert into #tt
values (iif(@test=1,(select min(tid) from #tu),(select max(tid) from #tu)))
select * from #tt
您的查询已修改
create procedure sp_m_ii_2_kaydet
@mtu int,
@mod int,
@moid int,
@uid int
as
insert into m_ii2_alisfatura
(muhtur, mturid, muhodm, modmid, uid) values
(@mtu,
IIF (@mtu = 3, (SELECT MAX(id) FROM m_ii1_vsf_personel),IIF(@mtu = 2,(SELECT MAX(id) FROM m_ii1_vsf_firma),
IIF(@mtu = 4,( SELECT MAX(id) FROM m_ii1_vsf_taseron),0))),
@mod, @moid, @uid)
答案 2 :(得分:0)
我使用这个组合代码得到了错误的MAX(ID)值;
...procedure [dbo].[sp_m_ii_2_kaydet]
@mtu int,
@mod int,
@uid int
as
declare @mtuid int
declare @modid int
if (@mtu = 3 and @mod = 1)
set @mtuid = (select MAX(id) from m_ii1_vsf_personel)
set @modid = (select MAX(id) from m_kasa_personel);
if (@mtu = 3 and @mod = 2)
set @mtuid = (select MAX(id) from m_ii1_vsf_personel)
set @modid = (select MAX(id) from m_hesap_personel);
if (@mtu = 3 and @mod = 3)
set @mtuid = (select MAX(id) from m_ii1_vsf_personel)
set @modid = (select MAX(id) from m_senet_personel);
if (@mtu = 3 and @mod = 4)
set @mtuid = (select MAX(id) from m_ii1_vsf_personel)
set @modid = (select MAX(id) from m_cekcikis_personel);
if (@mtu = 2 and @mod = 1)
set @mtuid = (select MAX(id) from m_ii1_vsf_firma)
set @modid = (select MAX(id) from m_kasa_firma);
if (@mtu = 2 and @mod = 2)
set @mtuid = (select MAX(id) from m_ii1_vsf_firma)
set @modid = (select MAX(id) from m_hesap_firma);
if (@mtu = 2 and @mod = 3)
set @mtuid = (select MAX(id) from m_ii1_vsf_firma)
set @modid = (select MAX(id) from m_senet_firma);
if (@mtu = 2 and @mod = 4)
set @mtuid = (select MAX(id) from m_ii1_vsf_firma)
set @modid = (select MAX(id) from m_cekcikis_firma);
if (@mtu = 4 and @mod = 1)
set @mtuid = (select MAX(id) from m_ii1_vsf_taseron)
set @modid = (select MAX(id) from m_kasa_taseron);
if (@mtu = 4 and @mod = 2)
set @mtuid = (select MAX(id) from m_ii1_vsf_taseron)
set @modid = (select MAX(id) from m_hesap_taseron);
if (@mtu = 4 and @mod = 3)
set @mtuid = (select MAX(id) from m_ii1_vsf_taseron)
set @modid = (select MAX(id) from m_senet_taseron);
if (@mtu = 4 and @mod = 4)
set @mtuid = (select MAX(id) from m_ii1_vsf_taseron)
set @modid = (select MAX(id) from m_cekcikis_taseron);
insert into m_ii2_alisfatura (muhtur, mturid, muhodm, modmid, uid) values
(@mtu, @mtuid, @mod, @modid, @uid)