在查询中调用存储过程

时间:2012-08-07 19:41:14

标签: sql sql-server stored-procedures

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

你有什么建议吗?

1 个答案:

答案 0 :(得分:8)

您无法在现有select语句中调用存储过程。你想要的是user defined scalar function(而不是表函数或内置函数)。

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