忽略&继续SQL Server查询 [英] Ignore & Continue in SQL Server Queries

查看:104
本文介绍了忽略&继续SQL Server查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家



考虑下面的数据库:

Hello Experts

Consider the below Database:

Create	Database	Test
Go
Use	Test
Go
Create	Table	table1	(
ID	BigInt	Identity	Not Null	Primary Key,
Title	nVarchar(50)		Not Null	Unique
)
Go
Insert	table1	(Title)
	Values	(N'title1')
Go





这是我的问题,我可以使用哪些代码来记录不兼容的记录并继续正确的记录。

例如考​​虑这个查询:



Here is my question that what code I can use to log the incompatible recordes and continue with correct records.
For example consider this query:

Insert	table1	(Title)
	Select	N'title2'
		Union
	Select	N'title1'  



如您所知,没有任何记录会影响。

是否有任何查询插入'title2'并打印插入错误'title1'?


As you know, no record will affect.
Is there any query that inserts the 'title2' and Prints the inserting error of 'title1' ?

推荐答案

你的问题不是足够清楚。



首先,我喜欢建议您阅读以下文章:

T-SQL中的错误处理:从休闲到宗教 [ ^ ]

在Transact-SQL中使用TRY ... CATCH [ ^ ]

SQL Server错误处理工作台 [ ^ ]

存储过程中的处理错误 [ ^ ]

SQL Server 2005尝试和捕获异常处理 [ ^ ]



其次,您可以通过以下三种方式检查现有记录:

1)使用 EXISTS [ ^ ]声明。

2)使用<$ h $ => http:// technet SELECT .microsoft.com / zh-CN / library / ms177682.aspx> IN [ ^ ]或 NOT IN 选项

3)循环播放记录集(使用 WHILE [ ^ ]语句或 CTE [ ^ ] /普通表格表格/)





示例:

Your issue is not clear enough.

First of all, i would suggest you to read below articles:
Error Handling in T-SQL: From Casual to Religious[^]
Using TRY...CATCH in Transact-SQL[^]
SQL Server Error Handling Workbench[^]
Handling Errors in Stored Procedures[^]
SQL Server 2005 Try and Catch Exception Handling[^]

Secondly, you can check for existing records in a 3 ways:
1) using EXISTS[^] statement.
2) using SELECT with IN[^] or NOT IN option
3) looping through the set of records (using WHILE[^] statement or CTE[^] /Common Table Expressions/)


Example:
INSERT INTO Table2 (Title)
SELECT Title
FROM (SELECT 'Title1' UNION ALL
      SELECT 'Title2' UNION ALL) AS T
WHERE T.Title NOT IN (SELECT Title FROM Table2)



注意:如果数据量很大,上面的查询不是最佳的,执行时间很长。


这篇关于忽略&amp;继续SQL Server查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆