Sql 在select语句中为变量赋值,然后用于在嵌套的select语句中查找值

Sql 在select语句中为变量赋值,然后用于在嵌套的select语句中查找值,sql,Sql,我发现了错误 为变量赋值的SELECT语句不得与数据检索操作结合使用 这是我的选择声明 SELECT A.vendor_id, **@vendor_employee** = A.vendor_employee_id , B.txt_first_name, B.txt_last_name, SELECT txt_Vendor_Employee_Detail_Element, (CASE WHEN txt_Vendor_Employee_D

我发现了错误

为变量赋值的SELECT语句不得与数据检索操作结合使用

这是我的选择声明

SELECT A.vendor_id, **@vendor_employee** = A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    SELECT txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN txt_Vendor_Employee_Detail_Value <> '' 
                AND txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
    FROM t_vendor_employee_detail 
    WHERE vendor_employee_id = **@vendor_employee**)

FROM...

这看起来像是可以使用连接重写的内容。在看不到整个查询的情况下,很难说怎么做,但下面是一个尝试:

SELECT A.vendor_id, A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    c.txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN c.txt_Vendor_Employee_Detail_Value <> '' 
                AND c.txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN c.txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, c.txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
FROM ...

INNER JOIN t_vendor_employee_detail c
ON c.vendor_employee_id = A.vendor_employee_id

正如错误消息所说,您不能以尝试的方式使用变量。

这看起来像是可以使用联接重写的内容。在看不到整个查询的情况下,很难说怎么做,但下面是一个尝试:

SELECT A.vendor_id, A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    c.txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN c.txt_Vendor_Employee_Detail_Value <> '' 
                AND c.txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN c.txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, c.txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
FROM ...

INNER JOIN t_vendor_employee_detail c
ON c.vendor_employee_id = A.vendor_employee_id

正如错误消息所说,您不能以尝试的方式使用变量。

是的,基本上您不能在同一select语句中返回数据和分配变量。我认为您要做的是引用外部值的相关子查询

那个世界看起来像这样:

SELECT A.vendor_id, A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    (SELECT d.txt_Vendor_Employee_Detail_Element
    FROM t_vendor_employee_detail d
    WHERE d.vendor_employee_id = A.Vendor_employee_id /* references outside the subqquery */)

FROM...
但是,您在子查询中也返回了多行,这些行可能应该重写为联接

SELECT A.vendor_id, A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    d.txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN D.txt_Vendor_Employee_Detail_Value <> '' 
                AND d.txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN d.txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, d.txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
FROM vendor_table_A A 
    INNER JOIN t_vendor_employee_detail d
         ON d.vendor_employee_id = A.vendor_employee_id
    INNER JOIN vendor_table_B B 
         ON...

这些示例将为您提供基本思路,但我们确实需要整个查询来为您提供完整的解决方案

是的,基本上不能在同一select语句中返回数据和分配变量。我认为您要做的是引用外部值的相关子查询

那个世界看起来像这样:

SELECT A.vendor_id, A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    (SELECT d.txt_Vendor_Employee_Detail_Element
    FROM t_vendor_employee_detail d
    WHERE d.vendor_employee_id = A.Vendor_employee_id /* references outside the subqquery */)

FROM...
但是,您在子查询中也返回了多行,这些行可能应该重写为联接

SELECT A.vendor_id, A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    d.txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN D.txt_Vendor_Employee_Detail_Value <> '' 
                AND d.txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN d.txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, d.txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
FROM vendor_table_A A 
    INNER JOIN t_vendor_employee_detail d
         ON d.vendor_employee_id = A.vendor_employee_id
    INNER JOIN vendor_table_B B 
         ON...

这些示例将为您提供基本思路,但我们确实需要整个查询来为您提供完整的解决方案

您可以使用merge语句检索和分配数据

SELECT A.vendor_id, **@vendor_employee** = A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    SELECT txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN txt_Vendor_Employee_Detail_Value <> '' 
                AND txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
    FROM t_vendor_employee_detail 
    WHERE vendor_employee_id = **@vendor_employee**)

FROM...
大概是这样的: 声明@ID表 ID INT


可以使用merge语句检索和分配数据

SELECT A.vendor_id, **@vendor_employee** = A.vendor_employee_id
    , B.txt_first_name, B.txt_last_name,
    SELECT txt_Vendor_Employee_Detail_Element, 
        (CASE 
            WHEN txt_Vendor_Employee_Detail_Value <> '' 
                AND txt_Vendor_Employee_Detail_Value IS NOT NULL 
            THEN txt_Vendor_Employee_Detail_Value 
            ELSE CONVERT(VARCHAR, txt_Vendor_Employee_Detail_Date) 
        END) AS Vendor_Detail_Element_Value 
    FROM t_vendor_employee_detail 
    WHERE vendor_employee_id = **@vendor_employee**)

FROM...
大概是这样的: 声明@ID表 ID INT


是的,我确实用过那个连接。我的问题是,我不知道我能识别出嵌套查询中的外部值。谢谢你的帮助,我确实用过这个加入。我的问题是,我不知道我能识别出嵌套查询中的外部值。谢谢你的帮助这解决了问题。我认为我不能在嵌套查询中使用外部值。谢谢,先生。这解决了问题。我认为我不能在嵌套查询中使用外部值。谢谢你,先生