SQL插入-选择语句
我将 Visual Basic 6.0 与 SQL Server 2005 一起使用,
这是我的代码:
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 语句,工作正常。两个输入直接保存到 [schedule] 表中,一个输入来自 [scheduletype] 表。
但是如果 cboSchedtype.Text 没有匹配记录怎么办?
SELECT st_id
FROM scheduletype
WHERE st_name = '" & cboSchedtype.Text & "'"
这是我想做的:
I. 仅当 [scheduletype] 表不存在时,才将 cboSchedtype.Text 的值创建为“子插入”(在主插入查询执行其操作之前)
II。否则继续正常。 (我的代码成功地做到了这一点。)
I use Visual Basic 6.0 with SQL Server 2005
Here is my code :
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 & "'"
This is an insert into select statement and works fine. Two inputs directly saved into the [schedule] table and one input coming from [scheduletype] table.
But what if there is no matching records for cboSchedtype.Text?
SELECT st_id
FROM scheduletype
WHERE st_name = '" & cboSchedtype.Text & "'"
Here's I want to do :
I. Make a 'sub-insert' for the value of cboSchedtype.Text into the [scheduletype] table only if it doesn't exist (before the main insert query does its thing)
II. Otherwise continue normally. (My code successfully does this.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
scheduletype
不存在,请使用它来添加它。完成此操作后,您可以针对
schedule
使用插入语句,因为您知道该行将存在。Use this to add a
scheduletype
if it does not exist.After you have done that you can use your insert statement against
schedule
because you know that the row will exist.ISNULL
:http://msdn.microsoft.com/ en-us/library/ms184325.aspxISNULL
: http://msdn.microsoft.com/en-us/library/ms184325.aspx