任何人都可以解释这个SQL代码的行为...... [英] Can Anyone Explain the Behaviour of this Sql Code......
本文介绍了任何人都可以解释这个SQL代码的行为......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DECLARE @Scrap_Qty int
DECLARE @FilledQty int
SET @Scrap_Qty=10 --Change this to 1 and it still Shows Same O/P
SET @FilledQty = 2
BEGIN
IF @Scrap_Qty > @FilledQty
declare @currDate varchar(10)
Set @currDate= Convert(VARCHAR(10),GetDate(),103)
PRINT @currDate
END
输出为:
The Output is:
07/06/2013
在所有情况下如果cond'n为False也显示o / p ......我知道没有开始
和结束
语句....但是无法理解这个行为如果stmt ...声明不是可执行文件...我知道......
任何解释都会有所帮助.....
(来自奇怪和精彩页面的原始问题发表于Virang_21)
(本代码不是由我写的......我不会写这种代码......)
in all cases if the cond'n is False also it's Showing the o/p...... I Know there is no Begin
and End
Statements.... But Unable to understand the Behaviour of this if stmt... Declare is not a Executable stmt.. i know...
Any Explanation Will be helpful.....
(Original Question from The Weird and Wonderful Page posted by Virang_21)
(This Code is not Written by me.. I Won't Ever write This Kind of Code...)
推荐答案
我认为您的BEGIN语句位于错误的位置,因此IF语句仅适用于声明@currDate varchar(10),这意味着接下来的两个语句将始终执行。 BEGIN语句应该是IF语句之后的第一个语句。
问候,
Ian。
I think your BEGIN statement is in the wrong place, so the IF statement only applies to the "declare @currDate varchar(10)", which means the next two statements will always execute. The BEGIN statement should be the first statement after the IF statement.
Regards,
Ian.
你好,
SQL Server没有块级变量作用域[ ^ 。因此,你的if语句导致什么都不执行,因为下一个语句是DECLARE并且是一个不可执行的语句。其余两个语句作为usal执行,因此你总是看到curDate的值被打印。
问候,
Hello,
SQL Server does not have block level variable scoping[^]. And hence your if statement results in executing nothing because the next statement is DECLARE AND is a non-executable statement. The rest two statements execute as usal and therefore you always see the value of curDate getting printed.
Regards,
你需要在你的if语句中添加括号如:IF(@Scrap_Qty> @FilledQty)
然后它将起作用
喜欢
You need to put brackets like : IF (@Scrap_Qty > @FilledQty)
in your if statement then it will work
like
IF (@Scrap_Qty > @FilledQty)
print 'y'
然后它将读取第一个语句
then it will read the first statement
这篇关于任何人都可以解释这个SQL代码的行为......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文