Sql server 如何根据传递给存储过程的参数更改SQL查询的标题?
我有一个存储过程,它接受值@OrderType,如果OrderType是Production,我希望标题作为ProductionDate,如果它是TakeDown,我希望标题作为TakedownDateSql server 如何根据传递给存储过程的参数更改SQL查询的标题?,sql-server,tsql,stored-procedures,Sql Server,Tsql,Stored Procedures,我有一个存储过程,它接受值@OrderType,如果OrderType是Production,我希望标题作为ProductionDate,如果它是TakeDown,我希望标题作为TakedownDate DECLARE @OrderType nvarchar(50); SELECT CONVERT(VARCHAR(10), FromDate, 101) AS [PostingDate] FROM table //如果ordertype为过帐,则输出为 PostingDate 1/1/
DECLARE @OrderType nvarchar(50);
SELECT CONVERT(VARCHAR(10), FromDate, 101) AS [PostingDate]
FROM table
//如果ordertype为过帐,则输出为
PostingDate
1/1/2018
TakedownDate
1/1/2018
//如果ordertype为TakeDown,则输出为
PostingDate
1/1/2018
TakedownDate
1/1/2018
也许这样的事情适合你:
CREATE PROCEDURE MyProcedure
@OrderType nvarchar(50)
AS
IF @OrderType = 'Production'
SELECT CONVERT(VARCHAR(10), FromDate, 101) AS [ProductionDate]
FROM table
ELSE IF @OrderType = 'TakeDown'
SELECT CONVERT(VARCHAR(10), FromDate, 101) AS [TakedownDate]
FROM table
ELSE
-- Perhaps some exception handling or a default value here...
;
请提供一些示例数据…..别名是标识符,不能在SQL中对标识符进行参数化。您唯一的选择是使用动态sql。注意-如果您有查询本身的参数,请不要使用
exec(@sql)
,而是构建一个适当的参数化查询,并使用exec sp\u executeSql
。