如果存在于子查询中的sql中

时间:2016-07-26 16:37:12

标签: sql sql-server if-statement exists

我正在尝试执行以下查询以检查来自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 个答案:

答案 0 :(得分:0)

首先,如果表中有任何记录,则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

答案 1 :(得分:0)

根据您的评论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