例如,当EXISTS没有显示我知道的值时,应该是假的

问题描述 投票:0回答:2

我需要帮助处理EXISTS时的情况,无论何时运行,它都会成功完成,但CASE WHEN Exists语句不显示正在进行的值,我知道它不等于0.此查询适用于我写的其他几个tsql语句但无论出于何种原因,都不在此。因此返回值,但显示为已完成,这是不正确的,因为其中一个设置实际上设置为1。


declare @STIG varchar(255)
SET @STIG = 'MS SQL Server 2016 Database'
declare @STIG1 varchar(255)
SET @STIG1 = 'MS SQL Server 2016 Instance'
DECLARE @VID2 varchar(20)
set @VID2 = 'V-71'


CREATE TABLE #temp2 ([DatabaseName] varchar(100), [DatabaseOwner] varchar(100), [is_trustworthy_on] varchar(5))

INSERT INTO [#temp2] (DatabaseName, DatabaseOwner, is_trustworthy_on)
SELECT name as DatabaseName,
SUSER_SNAME(owner_sid) AS DatabaseOwner, is_trustworthy_on
FROM sys.databases

SELECT @@servername as [HostName], @VID2 as [V-ID], @STIG AS [STIG], (CASE WHEN EXISTS (
    SELECT is_trustworthy_on
    FROM #temp2
    WHERE #temp2.is_trustworthy_on = 0)
Then CAST('Completed' AS varchar(12))
ELSE CAST ('Ongoing' AS varchar(12)) 
END) as [Status]

drop table #temp2

上面的OUTPUT应该是,

SERVEREXAMPLE \ Instance | V-71 | MS SQL Server ... |不断的

sql tsql ssms
2个回答
0
投票

如果这是你想要的,请检查is_trustworthy_on = 1

SELECT 
  @@servername as [HostName], 
  @VID2 as [V-ID], 
  @STIG AS [STIG], 
  CASE 
    WHEN EXISTS (
      SELECT 1 FROM #temp2 WHERE is_trustworthy_on = 1
    ) THEN 'Ongoing'
    ELSE 'Completed' 
  END as [Status]

1
投票

我想你想要这样的东西:

SELECT      @@servername as [HostName]
,           @VID2 as [V-ID]
,           @STIG AS [STIG]
,           DatabaseName
,           CASE 
                WHEN is_trustworthy_on = 0 
                    THEN 'Completed' 
                ELSE 'Ongoing' 
            END AS Status
FROM        #temp2
© www.soinside.com 2019 - 2024. All rights reserved.