2012-08-07 83 views
1

我想打電話給我屬我的查詢,但有2個錯誤:調用存儲過程中的查詢

Msg 156, Level 15, State 1, Line 17 
Incorrect syntax near the keyword 'EXEC'. 

Msg 156, Level 15, State 1, Line 19 
Incorrect syntax near the keyword 'AS'. 

這裏是我的查詢:

SELECT O.OrderId 
    ,O.Number 
    ,O.Creation 
    ,(SELECT Name + ' ' + Surname FROM [User] WHERE UserId = (SELECT CreatedBy FROM [User] WHERE UserId = O.UserId)) AS OrderOwner 
    ,(SELECT Name + ' ' + Surname FROM [User] WHERE UserId = O.UserId)AS Customer 
    ,(SELECT Telephone1 FROM [User] WHERE UserId = O.UserId) AS Telephone 
    ,(SELECT CASE IsActive WHEN 1 THEN 'Indirimli' WHEN 0 THEN 'Indirimsiz' END AS Indirim FROM [User] WHERE UserId = O.UserId) AS Discount 
    ,(SELECT CASE IsActive 
    WHEN 0 
    THEN 

     (SELECT SUM(Price) FROM Product WHERE ProductId IN(SELECT ProductId FROM OrderProduct WHERE OrderId = O.OrderId)) 
    WHEN 1 
    THEN 
    EXEC USP_CalculatePrice 70 

    END AS Price 
    FROM [User] WHERE UserId = O.UserID) AS Price 

    ,O.Description 
    ,O.Status 
    FROM 
    [Order] AS O 
    WHERE O.Status = 0 

你有什麼建議嗎?

+2

存儲過程不能像那樣工作。您可以使用存儲過程與查詢進行交互的唯一方法是執行「INSERT ... EXEC」。目前還不清楚該程序的功能是什麼......也許一個表值函數會更合適。 – 2012-08-07 19:44:22

+1

[SQL Server 2005:從WHERE子句調用存儲過程]的可能重複(http://stackoverflow.com/questions/943516/sql-server-2005-call-a-stored-procedure-from-a-where - 條款) – LittleBobbyTables 2012-08-07 19:45:20

+0

您可以向我們解釋ups_calculatePrice的功能嗎,所以我們可以給您最好的答案? – 2012-08-07 19:46:47

回答

8

您不能在現有的select語句中調用存儲過程。你想要的是user defined scalar function(與表函數或內置函數相反)。

根據您真正想要做什麼,可以應用用戶定義的表格功能。在這種情況下,你會創建一個返回表的函數,然後你可以加入它。他們有點像意見,但接受參數。