2013-12-10 114 views
-6

我正在創建一個函數,以便我可以將選擇語句的結果放入數據庫的視圖中。我的SELECT語句返回一個結果,當我運行它,但是當我去,並將其放置在創建一個功能我得到的錯誤:創建函數SQL Server 2012時出錯

Only one expression can be specified in the select list when the 
subquery is not introduced with EXISTS. 

我仍然新和學習,並希望在什麼我任何幫助做錯了。

CREATE FUNCTION CXi_Application 

@Application nvarchar(255), @Wave nvarchar(255) 

RETURNS int 
AS 
BEGIN 

DECLARE @Return int 


SET @Return = 
(SELECT @Application, (
(sum(CASE WHEN overall_score IN (1) THEN 1 ELSE 0 END) + sum(CASE WHEN effective_score IN (1) THEN 1 ELSE 0 END) + sum(CASE WHEN easeuse_score IN (1) THEN 1 ELSE 0 END)) + 
(sum(CASE WHEN overall_score IN (2) THEN 2 ELSE 0 END) + sum(CASE WHEN effective_score IN (2) THEN 2 ELSE 0 END) + sum(CASE WHEN easeuse_score IN (2) THEN 2 ELSE 0 END)) + 
(sum(CASE WHEN overall_score IN (3) THEN 3 ELSE 0 END) + sum(CASE WHEN effective_score IN (3) THEN 3 ELSE 0 END) + sum(CASE WHEN easeuse_score IN (3) THEN 3 ELSE 0 END)) + 
(sum(CASE WHEN overall_score IN (4) THEN 4 ELSE 0 END) + sum(CASE WHEN effective_score IN (4) THEN 4 ELSE 0 END) + sum(CASE WHEN easeuse_score IN (4) THEN 4 ELSE 0 END)) + 
(sum(CASE WHEN overall_score IN (5) THEN 5 ELSE 0 END) + sum(CASE WHEN effective_score IN (5) THEN 5 ELSE 0 END) + sum(CASE WHEN easeuse_score IN (5) THEN 5 ELSE 0 END)))/ 
(count(overall_score) + count(effective_score) + count(easeuse_score) * 1.00) 
FROM FY14_DataMerge 
WHERE [Status] = 'Completed' AND [Application] = @Application AND [Wave] = @Wave); 

-- Return the result of the function 
RETURN @Return 

END 
GO 
+2

的錯誤是不言自明的。運行您的'SELECT'語句並查看它輸出的列數。你只能有一個。 – Kermit

+0

是的,我現在看到了。謝謝你幫助我。 :) –

回答

7

你在DDL指定你想在函數返回一個INT

RETURNS int 

然而,你的結果集包括@Application和計算本身。基本上,你已經聲明你正在創建一個標量值函數,但是你的代碼試圖返回一個Table-Valued結果。首先,你需要決定你想要實際做什麼。

如果你返回一個結果(在調用語句的SELECT部分常見的),那麼很可能你正在尋找一個標量值函數,你需要從SELECT語句刪除@Application呼叫函數DDL。

如果你真的想從函數返回的值,那麼你將需要一個表值函數。此外,這是一個非常簡單的查詢,因此您可以將其製作爲內嵌TVF,其中有definite performance benefits,但我不確定它們在您的案例中將會如何清晰地實現。

下面是它看起來像做一個內聯TVF:

CREATE FUNCTION CXi_Application 

@Application nvarchar(255), @Wave nvarchar(255) 

RETURNS TABLE 
AS 
RETURN SELECT @Application as [Application], (
(sum(CASE overall_score WHEN 1 THEN 1 ELSE 0 END) 
    + sum(CASE effective_score WHEN 1 THEN 1 ELSE 0 END) 
    + sum(CASE easeuse_score WHEN 1 THEN 1 ELSE 0 END)) 
    + 
(sum(CASE overall_score WHEN 2 THEN 2 ELSE 0 END) 
    + sum(CASE effective_score WHEN 2 THEN 2 ELSE 0 END) 
    + sum(CASE easeuse_score WHEN 2 THEN 2 ELSE 0 END)) 
    + 
(sum(CASE overall_score WHEN 3 THEN 3 ELSE 0 END) 
    + sum(CASE effective_score WHEN 3 THEN 3 ELSE 0 END) 
    + sum(CASE easeuse_score WHEN 3 THEN 3 ELSE 0 END)) 
    + 
(sum(CASE overall_score WHEN 4 THEN 4 ELSE 0 END) 
    + sum(CASE effective_score WHEN 4 THEN 4 ELSE 0 END) 
    + sum(CASE easeuse_score WHEN 4 THEN 4 ELSE 0 END)) 
    + 
(sum(CASE overall_score WHEN 5 THEN 5 ELSE 0 END) 
    + sum(CASE effective_score WHEN 5 THEN 5 ELSE 0 END) 
    + sum(CASE easeuse_score WHEN 5 THEN 5 ELSE 0 END))) 
/
(count(overall_score) + count(effective_score) 
    + count(easeuse_score) * 1.00) as CalcMetric 
FROM FY14_DataMerge 
WHERE [Status] = 'Completed' AND [Application] = @Application AND [Wave] = @Wave; 
GO 

然而,如果5是每個數據點的最大值,那麼爲什麼不爲重構:

CREATE FUNCTION CXi_Application 

@Application nvarchar(255), @Wave nvarchar(255) 

RETURNS TABLE 
AS 
RETURN 
    SELECT @Application as [Application], 
     (SUM(overall_score) + SUM(effective_score) + SUM(easeuse_score)) 
     /
     (count(overall_score) + count(effective_score) 
      + count(easeuse_score) * 1.00) 
     FROM FY14_DataMerge 
     WHERE [Status] = 'Completed' 
      AND [Application] = @Application 
      AND [Wave] = @Wave; 
GO 
+0

不應該'@應用程序'的東西別名爲輸出,但? –

+0

@AndriyM是的。哎呦。謝謝 – swasheck

+0

非常感謝。我最終刪除了@Application,因爲我只是在存儲過程中使用它來引用,並且它工作正常。我感謝您的幫助。 –