1

我想在我的查询中调用我的 sp,但有 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

你有什么建议吗?

4

1 回答 1

9

您不能在现有select语句中调用存储过程。您想要的是用户定义的标量函数(而不是表函数或内置函数)。

根据您真正想要做的事情,可以应用用户定义的表函数。在这种情况下,您将创建一个返回表的函数,然后您可以加入它。它们有点像视图,但接受参数。

于 2012-08-07T19:44:34.617 回答