I want to call my sp in my query but there is 2 errors:
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'.
Here is my query:
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
Do you have any suggestion?
You can’t call stored procedures within an existing
selectstatement. What you want is user defined scalar function (as opposed to a table function or built in function).Depending on what you’re really trying to do, a user defined table function could apply. In that case, you’d make a function that returns a table and then you could join to it. They’re a bit like views but accept parameters.