SQL Server中的BEGIN ... END块 [英] BEGIN...END block in SQL Server
问题描述
在BEGIN...END
块下是否定义了用于对T-SQL语句进行分组的规则?
Are there any rules defined for grouping the T-SQL statements under the BEGIN...END
block ?
因为当我尝试CASE
语句或IIF
语句内的BEGIN...END
块时,它失败了.
Because when I try the BEGIN...END
block inside the CASE
statement OR IIF
statement, it fails.
我需要BEGIN...END
块,因为要在CASE
结果下执行多项操作.
I need BEGIN...END
block because there are multiple operations that I want to perform under the CASE
result.
SELECT
CASE @ChargePaid
WHEN 1 THEN
BEGIN
SELECT 'Paid'
END
WHEN 0 THEN
BEGIN
SELECT 'Not Paid'
END
END
OR
SELECT IIF( @ChargePaid > 0, BEGIN SELECT 'Paid' END , BEGIN SELECT 'Not Paid' END )
IF @cond = 'First'
WITH CTE AS (
SELECT 'A missing' Result
UNION
SELECT 'B missing' Result
UNION
SELECT 'C missing' Result
)
SET @msg = SELECT Result from CTE
IF @cond = 'Second'
WITH CTE AS (
SELECT 'A missing' Result
UNION
SELECT 'B missing' Result
UNION
SELECT 'C missing' Result
)
SET @msg = SELECT Result from CTE
IF @ChargePaid = 0
...
Some code goes here to generate the message.
Then I store the actual message into @msg variable.
...
In the end I store the @msg values (I trim the @msg if it requires) to the table.
我想要的是:
我想验证@ChargePaid条件. 如果错误,则我希望避免进一步处理以优化代码和&代码.将缺少的费用信息存储到@msg变量\表中.
I want to validate the @ChargePaid condition. If it comes false, I want to avoid further processing for optimization of code & store charge missing info to @msg variable\table.
推荐答案
BEGIN
/END
分隔程序声明.
包含一系列Transact-SQL语句,以便可以执行一组Transact-SQL语句.
Encloses a series of Transact-SQL statements so that a group of Transact-SQL statements can be executed.
CASE
块接受表达式.
计算条件列表并返回多个可能的结果表达式之一.
Evaluates a list of conditions and returns one of multiple possible result expressions.
因此,您正在尝试将方钉固定在圆孔中.
So you are trying to fit a square peg in a round hole.
这篇关于SQL Server中的BEGIN ... END块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!