我想知道如何在 SQL 的 CASE 语句中使用局部变量?
I would like to know how I can use local variables in CASE statements in SQL?
这个脚本给了我一个错误:
This script gives me an error:
DECLARE @Test int;
DECLARE @Result char(10);
SET @Test = 10;
CASE @Test
WHEN @Test = 10
THEN SET @Result='OK test'
END
Print @Result;
我使用 MS SQL 2008.
I use MS SQL 2008.
在 MSSQL 这个场景中使用 CASE 的两种方式
Two ways to use CASE in this scenario with MSSQL
DECLARE
@test int,
@result char(10)
SET @test = 10
SET @result = CASE @test
WHEN 10 THEN
'OK test'
ELSE
'Test is not OK'
END
PRINT @result;
SET @result = CASE
WHEN @test = 10 THEN
'OK test'
ELSE
'Test is not OK'
END
PRINT @result
这篇关于SQL CASE 和局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Sql server 表使用情况统计Sql server table usage statistics(Sql server 表使用情况统计)
t sql中的相对路径?Relative path in t sql?(t sql中的相对路径?)
在 WHERE 条件下获取 SQL 中的最后一条记录Getting the last record in SQL in WHERE condition(在 WHERE 条件下获取 SQL 中的最后一条记录)
在 SQL Server 中使用 FOR XML PATH 查询以获取分层数据Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查询以获取分层数据的 XML
嵌入在 sum() 函数中的 T-SQL IF 语句T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函数中的 T-SQL IF 语句)
表与临时表性能Table vs Temp Table Performance(表与临时表性能)