SQL Server存储过程中如何使用case语句
How to use case statement in SQLServer Stored procedure
我有一个像这样的存储过程:
Create PROCEDURE GetValidationData(
@ContextKey INT --Input parameter , Key to execute Statement
)AS
BEGIN
if (@ContextKey = 1)
begin
Select * from table A......
end
if (@ContextKey = 2)
begin
Some another select statement ......
end
.
.
.
.
.
.
.
.
.
like n
END
@ContextKey
是执行正确 Select 语句的参数
现在我想转换 CASE WHEN THEN 语句中的所有代码,我该怎么做?
在 @ContextKey
的基础上
你能试试这个吗
Create PROCEDURE GetValidationData(
@ContextKey INT --Input parameter , Key to execute Statement
)AS
BEGIN
declare @sql nvarchar(max)
select @sql = case @ContextKey
when 1 then 'Select * from table A'
when 2 then 'Select another select statement'
when number then 'Select other '
end
execute(@sql)
END
我有一个像这样的存储过程:
Create PROCEDURE GetValidationData(
@ContextKey INT --Input parameter , Key to execute Statement
)AS
BEGIN
if (@ContextKey = 1)
begin
Select * from table A......
end
if (@ContextKey = 2)
begin
Some another select statement ......
end
.
.
.
.
.
.
.
.
.
like n
END
@ContextKey
是执行正确 Select 语句的参数
现在我想转换 CASE WHEN THEN 语句中的所有代码,我该怎么做?
在 @ContextKey
你能试试这个吗
Create PROCEDURE GetValidationData(
@ContextKey INT --Input parameter , Key to execute Statement
)AS
BEGIN
declare @sql nvarchar(max)
select @sql = case @ContextKey
when 1 then 'Select * from table A'
when 2 then 'Select another select statement'
when number then 'Select other '
end
execute(@sql)
END