我在SQL Server 2005中使用Visual Basic 6.0
下面是我的代码:
Cn.Execute "INSERT INTO schedule (sch_name, st_id, sch_note)
SELECT '" & txtSchedname.Text & "', st_id, '" & txtNote.Text & "'
FROM scheduletype
WHERE st_name = '" & cboSchedtype.Text & "'"这是一条insert into select语句,运行良好。两个输入直接保存到明细表中,一个输入来自明细表。
但是,如果没有与cboSchedtype.Text匹配的记录怎么办?
SELECT st_id
FROM scheduletype
WHERE st_name = '" & cboSchedtype.Text & "'"这是我想做的:
I.仅当cboSchedtype.Text的值不存在时(在主insert查询执行其操作之前),才将其‘子插入’到调度类型表中
II.否则继续正常运行。(我的代码成功地做到了这一点。)
发布于 2011-08-04 14:56:31
如果scheduletype不存在,则使用此选项添加它。
insert into scheduletype
select 'TheScheduleType'
where not exists (select st_name
from scheduletype
where st_name = 'TheScheduleType')完成此操作后,您可以对schedule使用insert语句,因为您知道该行将存在。
发布于 2011-08-04 09:55:32
ISNULL:http://msdn.microsoft.com/en-us/library/ms184325.aspx
SELECT st_id FROM scheduletype WHERE st_name = ISNULL('" & cboSchedtype.Text & "', subquery)"https://stackoverflow.com/questions/6924012
复制相似问题