SQL:如何知道数据库中是否存在一组值?
我必须将文件中的行导入数据库。我必须检查外键是否存在。这里是我应该导入的行示例:
A 481 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 482 11/23/1981 15 77000 DESCRIPTION_1 $5,098
A 482 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 9 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
这里外键是 12, 15, 9, 12, 1。我想检查这些外键是否是其中之一。在示例中,外键 12 不存在。最简单的方法是逐行测试:
SELECT COUNT(*) From TypeTable WHERE IdType = 12 -- 0
SELECT COUNT(*) From TypeTable WHERE IdType = 15 -- 1
SELECT COUNT(*) From TypeTable WHERE IdType = 9 -- 1
SELECT COUNT(*) From TypeTable WHERE IdType = 12 -- 1
SELECT COUNT(*) From TypeTable WHERE IdType = 1 -- 1
此方法的问题是每个不同的外键都有一个查询,而我应该导入的文件有数千行(可能还有数千行外键)。
所以我想知道是否可以使用“SQL的设置功能”在一个查询中检索,但我不知道如何做到这一点...
PS:在我的查询结果中,我需要知道外国数据库中不存在密钥
问候,
Florian
I must import lines from a file into database. I must check if foreign key exists. Here a sample of lines I should import :
A 481 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 482 11/23/1981 15 77000 DESCRIPTION_1 $5,098
A 482 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 9 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 12 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
A 481 11/23/1981 1 77000 DESCRIPTION_1 $5,098
Here the foreign key are 12, 15, 9, 12, 1. I want to check if one of these foreign key. In the sample the foreign key 12 does not exist. The simplest way is to test line by line :
SELECT COUNT(*) From TypeTable WHERE IdType = 12 -- 0
SELECT COUNT(*) From TypeTable WHERE IdType = 15 -- 1
SELECT COUNT(*) From TypeTable WHERE IdType = 9 -- 1
SELECT COUNT(*) From TypeTable WHERE IdType = 12 -- 1
SELECT COUNT(*) From TypeTable WHERE IdType = 1 -- 1
The problem with this method is that there is one query for each different foreign key, and files I should import has thousands lines (and potentially thousands foreign key).
So I want to know if it's possible to use the "set feature of SQL" to retrieve in one query but I don't know how to do this...
PS : In the result of my query, I need to know the foreign key does not exist in database
Regards,
Florian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果数据已加载到临时表中,那么您可以使用此表
来执行基于集合的操作,您需要一个具有所需 FK 值的表。这可能是同一连接上的临时表,并通过扫描文件进行填充。通过将
myStagingTable
更改为#myFKTable
,相同的 SQL 也可以在上面工作If the data has been loaded into a staging table then you can use this
To do something set based, you need a table with the desired FK values. This could be a temp table on the same connection and populated by scanning the file. The same SQL would work above by changing
myStagingTable
to#myFKTable
我会将数据批量加载到数据库中的临时暂存表中,然后它就成为运行带有外连接的 SELECT 的简单情况:
I would bulk load the data into a temporary staging table in the database, and then it becomes a simple case of running a SELECT with an outer join:
参数化您的查询并将外键作为参数传递?
如果将其放入函数中,则可以使用
CROSS APPLY
命令为集合的每一行调用该函数。Parameterise your query and pass the foreign key as a parameter?
If you put this into a function, you can use the
CROSS APPLY
command to call into the function for each row of a set.