【问题标题】:if exists in sql within a subquery如果存在于子查询中的 sql 中
【发布时间】:2016-11-30 10:18:00
【问题描述】:

我正在尝试执行以下查询以检查 4 个表中的记录,然后调用一个函数,但在括号附近出现错误。如果我用例它工作正常,但在满足第一个条件时退出.我需要评估 4 个 IF 中的所有表格:

select account_id,
        (
        if exists (select account_id from [dbo].[TEST_R6]) 
        Begin
        select  dbo.make_indicator(cent,chem,co,dim,lg,pl,strs,vis)  + space(1) + 'rr'
        End


if exists (select account_id from tbl_noR6) 
   begin
      select dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2) + 'cc'
  end


if exists (select account_id from tbl_acct) 
   begin
                 select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(3) + 'pp'
   end

if exists (select account_id from test_con) 
   begin
                select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(4) + 'no'
   end

     )as value from CRS_PRODLINE

代码部分与 case 语句一起工作,仅在满足第一种情况时给出输出,不检查其他情况:

DECLARE @cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.A_NAME) 
        FROM TEST_DEL c
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

SET @query = 'SELECT account_id, ' + @cols + '  from 
        (select   account_id,
                (case
                when exists (select  account_id from [dbo].[TEST_R6]) 
                then 
                dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis)  + space(1) 


                when exists (select account_id from tbl_noR6) 
                then 
                dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2)

                when exists (select account_id from tbl_acct) 
                then 
                dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2)

            end) as value,
            assay_name
            from CRS_PRODLINE
       ) x
        pivot 
        (
            MAX(newvalue)
            for a_name in (' + @cols + ')
        ) p '

execute(@query)

【问题讨论】:

  • 你上面写的查询是错误的。你能提供你的用例你想要达到的目标吗?举个例子
  • 这很有帮助,这已经是动态查询的一部分,我在其中透视从这些表中获得的数据。我真正需要的只是根据它所在的表调用具有指定空格的函数。例如,如果帐户存在于第一个表中,那么该函数应该给它的输出一个空格,如果在第二个表中有 2空格等等!我正在添加部分适用于 case 语句的代码,但与 sql select 一样,只要满足第一个条件,它就会失效!

标签: sql sql-server if-statement exists


【解决方案1】:

首先,如果表中有任何记录,您的exists 子句将返回true。我会假设这是故意的,您并没有尝试匹配特定的 account_id。

如果您想使用这种结构,您将不得不使用动态 SQL。像这样的:

DECLARE @Sql VARCHAR(8000)
IF EXISTS (SELECT account_id FROM tbl_noR6)
BEGIN
    SET @Sql = 'select dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2) + ''cc'''
    SET @Sql = @Sql + ')as value from CRS_PRODLINE'
    EXEC @Sql
END

IF EXISTS (SELECT account_id FROM tbl_acct)
BEGIN
    SET @Sql = 'select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(3) + ''pp'''
    SET @Sql = @Sql + ')as value from CRS_PRODLINE'
    EXEC @Sql
END

IF EXISTS (SELECT account_id FROM test_con)
BEGIN
    SET @Sql = 'select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(4) + ''no'''
    SET @Sql = @Sql + ')as value from CRS_PRODLINE'
    EXEC @Sql
END

【讨论】:

  • 这很有帮助,这已经是动态查询的一部分,我在其中透视从这些表中获得的数据。我真正需要的只是根据它所在的表调用具有指定空格的函数。例如,如果帐户存在于第一个表中,那么该函数应该给它的输出一个空格,如果在第二个表中有 2空格等等!我正在添加部分适用于 case 语句的代码,但与 sql select 一样,只要满足第一个条件,它就会失效!
【解决方案2】:

根据您的评论,即 CASE 只评估一次问题,这应该构建一个包含所有适用案例的字符串。

select account_id,
(
    '' 
    +
    case when exists (select account_id from dbo.[TEST_R6]) THEN select dbo.make_indicator(cent,chem,co,dim,lg,pl,strs,vis)  + space(1) + 'rr' ELSE '' END
    +
    case when exists (select account_id from dbo.tbl_noR6) THEN select dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2) + 'cc' ELSE '' END
    +
    case when exists (select account_id from dbo.tbl_acct) THEN select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(3) + 'pp' ELSE '' END
    +
    case when exists (select account_id from dbo.test_con) THEN select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(4) + 'no' ELSE '' END    
 ) as value

from CRS_PRODLINE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2015-02-26
    相关资源
    最近更新 更多