Sql server 将IF..ELSE与multi INSERT一起使用到Using子查询中
我在数据库上有两个表通过外键相互关联 表1是期刊 表2是日志 我有一个查询来检查表journal中当前日期是否有行。如果有记录,它将向journalEntries插入记录 如果为空,则将在日记账表1上插入记录,然后插入日记账表2 我尝试了不同的方法,但不起作用Sql server 将IF..ELSE与multi INSERT一起使用到Using子查询中,sql-server,Sql Server,我在数据库上有两个表通过外键相互关联 表1是期刊 表2是日志 我有一个查询来检查表journal中当前日期是否有行。如果有记录,它将向journalEntries插入记录 如果为空,则将在日记账表1上插入记录,然后插入日记账表2 我尝试了不同的方法,但不起作用 if (select j_id from journal where [date] > (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) is null insert into journal
if (select j_id from journal where [date] > (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) is null
insert into journal values (getdate() as [date], getdate() as [insert_date], '' as notes)
insert into journalentries (j_id, acc_num, credit, debit, [user_id], note)
select (select (select j_id from Journal where [date] in (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) as j_id, @acc_num, @credit, @debit, @user_id, @note)
else
insert into journalentries (j_id, acc_num, credit, debit, [user_id], note)
select (select j_id from Journal where [date] in (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) as j_id, @acc_num, @credit, @debit, @user_id, @note
end
另一种方式
if(select j_id from journal where [date] in ('2019-01-01')) is null
insert journal([date], insert_date, notes)
OUTPUT inserted.j_id, '2019-01-01', '2019-01-01', ''
INTO dbo.journalentries(j_id, acc_num, credit, debit, [user_id], note)
values (j_id, @acc_num, @credit, @debit, @user_id, @note)
else
select (select j_id from Journal where [date] in (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) as j_id, @acc_num, @credit, @debit, @user_id, @note
就像一个指针,太长了,不适合评论
if (select j_id from journal where [date] > (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) is null
begin
insert into journal select getdate() as [date], getdate() as [insert_date], '' as notes;
--insert into journalentries j_id, acc_num, credit, debit, [user_id], note); '!! where are you selecting this from?
select (select (select j_id from Journal where [date] in (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) as j_id, @acc_num, @credit, @debit, @user_id, @note);
end
else
begin
--insert into journalentries (j_id, acc_num, credit, debit, [user_id], note) -- !! where are you selecting this from?
select (select j_id from Journal where [date] in (SELECT CONVERT(VARCHAR(10),GETDATE(),111))) as j_id, @acc_num, @credit, @debit, @user_id, @note
end
我使用了scop_identity(),它很有效
declare @j_id int = (select j_id from journal where [date] in (SELECT CONVERT(VARCHAR(10),GETDATE(),111)))
if @j_id is null
begin
insert into journal values (getdate(), getdate(), '') set @j_id = SCOPE_IDENTITY(); insert into journalentries (j_id, acc_num, credit, debit, [user_id], note) values (@j_id, @acc_num, @credit, @debit, @user_id, '');
end
else
begin
insert into journalentries (j_id, acc_num, credit, debit, [user_id], note)values (@j_id, @acc_num, @credit, @debit, @user_id, '')
end
什么不起作用?有什么特别的错误信息吗?您是否需要在IF…..中包含块。。。。。其他的使用BEGIN和END?insert语句不起作用,它没有添加记录。它在第一个块中给我一个语法错误。小心,如果第一个查询返回多个结果,我将使用
MAX(j_id)
或TOP 1 j_id
,以确保安全