如何在 IF“子句”中做多项事情?我真的可以只做一个动作吗?我在谷歌上搜索了“sql server scope”,但它返回了一堆关于“scope_identity”的无关内容,而没有关于语法......
例子:
DECLARE @db_level int = 2
DECLARE @db_major int = 1
DECLARE @db_minor int = 0
DECLARE @db_point int = 0
DECLARE @db_current_level int = (SELECT MAX(db_level) FROM db_schema_log_tb)
IF (@db_level > @db_current_level)
PRINT 'Script failed because database level is higher than script level';
GOTO ExitScript;
ELSE
SELECT * FROM custom_tb;
ExitScript:
Run Code Online (Sandbox Code Playgroud)
显然,这个脚本中的 ELSE 是不正确的语法,除非我删除了 PRINT 语句。
Aar*_*and 11
您需要一个块构造 ( BEGIN
/ END
)。就个人而言,我认为即使您只有一个操作,也应该始终使用它们。
IF (somecondition)
BEGIN
-- do one thing
-- do another thing
-- do a third thing
END
ELSE
BEGIN
-- do the other thing(s)
END
Run Code Online (Sandbox Code Playgroud)