且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么五个数之和1 = 4

更新时间:2022-12-09 20:27:38

这是原因(如 Twitter上所述):

变量@last_task是在单独的查询批"中定义的.我将对SQL Fiddle的查询分解为单独的批处理,分别执行.我这样做是为了您可以在下面看到每个批次的输出作为不同的结果集.在您的小提琴中,您可以看到有两组输出: http://sqlfiddle. com/#!2/e80f5/3/0 http://sqlfiddle .com/#!2/e80f5/3/1 .这些映射到您正在运行的两个语句(集合和选择).问题是,您的set语句定义了仅在第一批中存在的变量;当select语句运行时,它是一个单独的批处理,并且您的变量未在该上下文中定义.

The variable @last_task was defined in a separate query "batch". I break up the queries on SQL Fiddle into individual batches, executed separately. I do this so you can see the output from each batch as a distinct result set below. In your Fiddle, you can see that there are two sets of output: http://sqlfiddle.com/#!2/e80f5/3/0 and http://sqlfiddle.com/#!2/e80f5/3/1. These map to the two statements you are running (the set and the select). The problem is, your set statement defines a variable that only exists in the first batch; when the select statement runs, it is a separate batch and your variable isn't defined within that context.

要解决此问题,您所要做的就是定义一个不同的查询终止符.请注意模式和查询面板([;])下的下拉框/按钮-单击它,您可以选择除分号(默认值)以外的其他内容.然后,您的两个语句将作为同一批处理的一部分一起包含在内,您将获得所需的结果.例如: http://sqlfiddle.com/#!2/e80f5/9

To correct this problem, all you have to do is define a different query terminator. Note the dropdown box/button under both the schema and the query panels ( [ ; ] ) - click on that, and you can choose something other than semicolon (the default). Then your two statements will be included together as part of the same batch, and you'll get the result you want. For example: http://sqlfiddle.com/#!2/e80f5/9