[英]while and if condition within a case statement t-sql
I am trying to implement a while loop and if condition (with a function call) in a case statement. 我正在尝试在case语句中实现while循环和if条件(使用函数调用)。 Basically, I am trying to do this:
基本上,我正在尝试这样做:
begin
case
when (condition1 and condition 2 and ncolumn_num between 6 and 9)
then
while @i < 10
begin
if(hsip.Is_numeric( hsip.getTempResponseById(@cregion, @cState_code, @nFY, @nReport_id, @nsection_id, @nquestion_number, @ndisplay_number, @nquestion_number, @nquestion_part_number, @suser_id, @nrow_number, i, @suser_id)))
@nrunningtotal = @nrunningtotal + hsip.getTempResponseById(@cRegion, @cState_code, @nFY, @nReport_id, @nsection_id, @nquestion_number, @ndisplay_number, @nquestion_number,
@nquestion_part_number, @suser_id, @nrow_number, @ncolumn_number, @suser_id)
end if
end
Can I implement while loop and if condition within case statement. 我是否可以在case语句中实现while循环和if条件。 My business logic requires me to total the columns from 6 to 9 and have final total in col:10.
我的业务逻辑要求我将第6列到第9列合计,并在col:10中获得最终合计。
I have 2 functions: gettempresponsebyid
: which returns response string and isnumeric
function which converts to number. 我有2个功能:
gettempresponsebyid
:返回响应字符串和isnumeric
函数转换为数字的。
Can someone please suggest a better way of doing this. 有人可以建议一种更好的方法吗? I get this error:
我收到此错误:
INCORRECT SYNTAX NEAR IF AND INCORRECT SYNTAX NEAR RUNNINGTOTAL.
如果RUN附近的语法错误,并且RUNNINGTOTAL附近的语法错误。
Q: Can I implement while loop and if condition within case statement. 问:我是否可以在case语句中实现while循环和if条件。
A: No. CASE
is not a T-SQL statement . 答:不可以
CASE
不是T-SQL 语句 。 It's an expression . 这是一种表达 。
https://msdn.microsoft.com/en-us/library/ms181765.aspx https://msdn.microsoft.com/zh-CN/library/ms181765.aspx
In the posted code, one big syntax problem starts right here: 在发布的代码中,一个大语法问题就从这里开始:
BEGIN CASE ...
^
Use the IF / ELSE
statement to conditionally control flow in T-SQL. 使用
IF / ELSE
语句有条件地控制T-SQL中的流。
https://msdn.microsoft.com/en-us/library/ms182717.aspx https://msdn.microsoft.com/zh-CN/library/ms182717.aspx
For a start, replace the invalid usage of the CASE
expression with an IF
statement. 首先,用
IF
语句替换CASE
表达式的无效用法。
BEGIN
IF (condition1 AND condition2 AND ncolumn_num between 6 and 9)
BEGIN
...
END;
END IF;
END;
But even with that change, it's still not clear what you are attempting to achieve or what problem you are trying to solve. 但是即使进行了这种更改,也仍然不清楚您要实现的目标或要解决的问题。 There is likely a much better way to do what you are trying to do, but we'd just be throwing out guesses at what you are trying to do.
可能有更好的方法来做您想做的事情,但是我们只是在猜测您要做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.