SQL:如何知道数据库中是否存在一组值?

发布于 2024-11-08 21:15:22 字数 1195 浏览 0 评论 0原文

我必须将文件中的行导入数据库。我必须检查外键是否存在。这里是我应该导入的行示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱我好吗 2024-11-15 21:15:22

如果数据已加载到临时表中,那么您可以使用此表

SELECT
    *
FROM
   myStagingTable ST
WHERE
   NOT EXISTS (SELECT *
            FROM TypeTable TT
            WHERE ST.FKCol = TT.IdType)

来执行基于集合的操作,您需要一个具有所需 FK 值的表。这可能是同一连接上的临时表,并通过扫描文件进行填充。通过将 myStagingTable 更改为 #myFKTable,相同的 SQL 也可以在上面工作

If the data has been loaded into a staging table then you can use this

SELECT
    *
FROM
   myStagingTable ST
WHERE
   NOT EXISTS (SELECT *
            FROM TypeTable TT
            WHERE ST.FKCol = TT.IdType)

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

无言温柔 2024-11-15 21:15:22

我会将数据批量加载到数据库中的临时暂存表中,然后它就成为运行带有外连接的 SELECT 的简单情况:

SELECT DISTINCT s.ForeignKeyField
FROM YourStagingTable s
    LEFT JOIN FKTable f ON s.ForeignKeyField = f.ID
WHERE f.ID IS NULL -- where the foreign key value does not exist

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:

SELECT DISTINCT s.ForeignKeyField
FROM YourStagingTable s
    LEFT JOIN FKTable f ON s.ForeignKeyField = f.ID
WHERE f.ID IS NULL -- where the foreign key value does not exist
Bonjour°[大白 2024-11-15 21:15:22

参数化您的查询并将外键作为参数传递?

IF EXISTS (SELECT * FROM TypeTable WHERE IdType = @foreignKey)

如果将其放入函数中,则可以使用CROSS APPLY 命令为集合的每一行调用该函数。

Parameterise your query and pass the foreign key as a parameter?

IF EXISTS (SELECT * FROM TypeTable WHERE IdType = @foreignKey)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文