2016-07-26 55 views
0

我試圖執行以下查詢來檢查4個表中的記錄,然後調用一個函數,但我得到一個附近的括號錯誤。它工作正常,如果我使用的情況下,但當滿足第一個條件時退出。我需要評估在4 IF的所有表:如果存在子查詢中的sql

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) 
+0

你上面寫的查詢是錯誤的。你能否提供你的用例,你想達到什麼目的。舉例 –

+0

這有幫助,這已經是動態查詢的一部分,我將從這些表中獲取的數據進行透視。我真正需要的只是調用具有指定空格的函數,具體取決於它所在的表。例如,如果帳戶存在於第一個表中,那麼函數應該給它輸出1個空格,如果在第二個表中有2個空間等!我添加了部分與case語句一起工作的代碼,但是與sql一樣,select一旦遇到第一個條件就會退出! –

回答

0

首先,如果表中有任何記錄,您的存在子句將返回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 
+0

這有幫助,這已經是動態查詢的一部分,我將從這些表中獲取的數據進行透視。我真正需要的只是調用具有指定空格的函數,具體取決於它所在的表。例如,如果帳戶存在於第一個表中,那麼函數應該給它輸出1個空格,如果在第二個表中有2個空間等!我添加了部分與case語句一起工作的代碼,但是與sql一樣,select一旦遇到第一個條件就會退出! –

0

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