Sql server SQL-为select语句中的所有查询行设置第三列值

Sql server SQL-为select语句中的所有查询行设置第三列值,sql-server,stored-procedures,Sql Server,Stored Procedures,我一直在寻找解决我问题的办法,但毫无结果。我有一个选择某些数据的存储过程。我被告知要修改它以添加一列“状态”。现在,此存储过程一次选择一个组件的信息。对于组件A:我有以下查询和结果: Declare @comp varchar(175) Select CT.ComponentPart CL.ComponentSupplier, CS.ComponentSource From ComponentParts CT inner join ComponentSo

我一直在寻找解决我问题的办法,但毫无结果。我有一个选择某些数据的存储过程。我被告知要修改它以添加一列“状态”。现在,此存储过程一次选择一个组件的信息。对于组件A:我有以下查询和结果:

 Declare @comp varchar(175)

 Select CT.ComponentPart
        CL.ComponentSupplier,
        CS.ComponentSource
 From ComponentParts CT
 inner join ComponentSource CS on CS.ComponentID=CT.ComponentID
 inner join ComponentSupplier CL on CL.ComponentTypeID=CS.ComponentTypeID
 Where CT.ComponentPart = @Comp
现在,上述查询的结果是:



我被要求在此查询中添加另一列,称为“组件状态”,如果其中一个PLNT不来自某个列表,则我会将整个列的值设置为“过时”,如果该特定组件的所有工厂都在列表中,则我会将该列设置为“活动”。现在我制作了一个表格,并在其中添加了植物列表和一个ID字段(列名:PlntID,PLNTSource)

但我遇到的问题是,我在上面的查询中尝试左键连接该表,并且在select子句中有一个case语句,其中如果PLNTSource字段为null,则设置为OVERSED else Active,但我无法根据查询中的列表将整个“status”列设置为一个值。是否仍要这样做?下面是我得到的

Declare @comp varchar(175)

 Select CT.ComponentPart
    CL.ComponentSupplier,
    CS.ComponentSource,
    CASE
      WHEN CST.PLNtSource IS NULL
        THEN 'obsolete'
        else 'Active'
    END as 'Status'
From ComponentParts CT
inner join ComponentSource CS on CS.ComponentID=CT.ComponentID
inner join ComponentSupplier CL on CL.ComponentTypeID=CS.ComponentTypeID
left join ComponentStat CST on CST.PlNtSource=CS.ComponentSource
Where CT.ComponentPart = @Comp
查询结果:

    ComponentPart    ComponentSupplier       ComponentSource    Status
     CAF123              Supplier1                PLNT1         Active
     CAF123              Supplier2                PLNT2         Obsolete
     CAF123              Supplier3                PLNT3         Obsolete
     CAF123              Supplier4                PLNT1         Active
我试图达到的结果是:

 ComponentPart    ComponentSupplier       ComponentSource    Status
     CAF123              Supplier1                PLNT1         Obsolete
     CAF123              Supplier2                PLNT2         Obsolete
     CAF123              Supplier3                PLNT3         Obsolete
     CAF123              Supplier4                PLNT1        Obsolete

有什么想法吗?

使用一个临时表,将结果存储在一个带有列状态的临时表中,并通过在if exists条件中将列状态设置为Observe来更新表


您可以使用if exists条件并将值硬编码为“过时”或声明一个变量,并将其与您的条件一起设置为“过时”或“活动”,并在select语句中使用它

我可以建议以下方法,使用CTE使事情更具可读性和可管理性。请注意,SQL Server中的CTE可以使用变量,只要它们是在同一批中定义的。由于您对整个查询的状态确定是相同的,因此我们可以使用子查询来计算该值,然后在原始查询中使用它

WITH cte AS (
    SELECT CT.ComponentPart,
           CL.ComponentSupplier,
           CS.ComponentSource,
           CST.PLNtSource
    FROM ComponentParts CT
    INNER JOIN ComponentSource CS
        ON CS.ComponentID = CT.ComponentID
    INNER JOIN ComponentSupplier CL
        ON CL.ComponentTypeID = CS.ComponentTypeID
    LEFT JOIN ComponentStat CST
        ON CST.PlNtSource = CS.ComponentSource
    WHERE CT.ComponentPart = @Comp
)
SELECT t1.ComponentPart,
       t1.ComponentSupplier,
       t1.ComponentSource,
       (SELECT CASE WHEN SUM(CASE WHEN t2.PLNtSource IS NULL THEN 1 ELSE 0 END) > 0
                    THEN 'Obsolete'
                    ELSE 'Active' END
        FROM cte t2) AS Status
FROM cte t1

好的,
PLNtSource
不能在您期望的任何地方都为
NULL
。检查您的源数据。@TimBiegeleisen是的,我知道我想根据PlntSource是否有一个空值来设置整个列的状态,因此如果其中一个值为空,我就去将整个列的状态设置为过时,如果没有空值,那么activehello,感谢您的响应,我在考虑这个选项,但我不能根据订单在这个过程中创建临时表,但我在想,我可以用函数还是不用函数?我不确定如何更新select查询列是否可行?Hi@eto_donna您可以使用if exists条件并将值硬编码为“过时”,或者声明一个变量并将其与您的条件一起设置为“过时”或“活动”,并在select语句中使用它
WITH cte AS (
    SELECT CT.ComponentPart,
           CL.ComponentSupplier,
           CS.ComponentSource,
           CST.PLNtSource
    FROM ComponentParts CT
    INNER JOIN ComponentSource CS
        ON CS.ComponentID = CT.ComponentID
    INNER JOIN ComponentSupplier CL
        ON CL.ComponentTypeID = CS.ComponentTypeID
    LEFT JOIN ComponentStat CST
        ON CST.PlNtSource = CS.ComponentSource
    WHERE CT.ComponentPart = @Comp
)
SELECT t1.ComponentPart,
       t1.ComponentSupplier,
       t1.ComponentSource,
       (SELECT CASE WHEN SUM(CASE WHEN t2.PLNtSource IS NULL THEN 1 ELSE 0 END) > 0
                    THEN 'Obsolete'
                    ELSE 'Active' END
        FROM cte t2) AS Status
FROM cte t1