简体   繁体   English

SQL-使用XML路径查询

[英]SQL - Query using XML Path

I have the following query: 我有以下查询:

SELECT     ara.req_id
FROM        tbl_ara ara
WHERE       id in (
                        SELECT STUFF((
                        SELECT ',' + cast(id as varchar(8000))
                        FROM contact
                        WHERE ct_id = 2
                        FOR XML PATH('')
                        ), 1, 1, ''))

So, theres is table with name contact that I want to get all the ids when they are type of 2 . 因此,有一个名称为contact表,当它们的类型为2时,我想获取所有ids Then I am creating a list with Stuff. 然后用Stuff创建一个列表。 Then I want to use another table and get all the records that their id is on that list. 然后,我想使用另一个表并获取其id在该列表上的所有记录。 You can see the logic in the query. 您可以在查询中看到逻辑。 The problem is that SQL doesn't recognise it as list and I get the following error: 问题是SQL无法将其识别为列表,并且出现以下错误:

Msg 245, Level 16, State 1, Line 11 Conversion failed when converting the nvarchar value '6019,49111,49112' to data type int. 消息245,级别16,状态1,行11将nvarchar值'6019,49111,49112'转换为数据类型int时转换失败。

STUFF in your case returns character data. 在您的情况下, STUFF返回字符数据。 From the documentation ( https://msdn.microsoft.com/en-us/library/ms188043.aspx ): 从文档( https://msdn.microsoft.com/en-us/library/ms188043.aspx ):

Returns character data if character_expression is one of the supported character data types. 如果character_expression是受支持的字符数据类型之一,则返回字符数据。 Returns binary data if character_expression is one of the supported binary data types. 如果character_expression是受支持的二进制数据类型之一,则返回二进制数据。

So you do not get a list of IDs but rather a string containing numbers and commas. 因此,您不会得到ID的列表,而是得到包含数字和逗号的字符串。

Is there a reason for not using the IDs directly? 是否有不直接使用ID的原因?

SELECT 
    [ara].[req_id]
FROM [tbl_ara] [ara]
WHERE [id] IN
             (
              SELECT 
                  [id]
              FROM [contact]
              WHERE [ct_id] = 2
             )

EDIT: Of course the JOIN solution by @gofr1 is a pretty good option, too. 编辑:当然,@ gofr1的JOIN解决方案也是一个不错的选择。

The simplest way is JOIN: 最简单的方法是JOIN:

SELECT ara.req_id
FROM tbl_ara ara
INNER JOIN contact c
    ON ara.id = c.id
WHERE c.ct_id = 2

If you want to run using STUFF then you should use dynamic SQL. 如果要使用STUFF运行,则应使用动态SQL。 At first prepare statement, then execute it: 首先准备语句,然后执行它:

DECLARE @sql nvarchar(max), @inpart nvarchar(max)

SELECT @inpart = STUFF((
                    SELECT ',' + cast(id as varchar(8000))
                    FROM contact
                    WHERE ct_id = 2
                    FOR XML PATH('')
                    ), 1, 1, '')


SELECT @sql = '
SELECT     ara.req_id
FROM        tbl_ara ara
WHERE       id in (' + @inpart+ ')'


PRINT @sql

--EXEC sp_executesql @sql

PRINT would get you this: PRINT将为您提供:

SELECT     ara.req_id
FROM        tbl_ara ara
WHERE       id in (6019,49111,49112)

Uncomment EXEC sp_executesql @sql to execute query. 取消注释EXEC sp_executesql @sql以执行查询。

Why don't you try this : 你为什么不试试这个:

SELECT  ara.req_id
FROM    tbl_ara As ara
WHERE   Exists( Select  1
                From    contact As c
                Where   c.ct_id = 2
                        And c.id = ara.id
              )

As exists clause only check the existence of single match and return that row when match found. As exist子句仅检查单个匹配项的存在,并在找到匹配项时返回该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM