Quantcast
Channel: SQL.ru: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 7251

Поведение переменной в update

$
0
0
Добрый день

MS SQL Server 2012
create table test (f1 int identity(1, 1), f2 int, f3 int)

declare @v int

insert into test (f2, f3)
values (null, null), (null, null), (null, null), (null, null), (null, null), (null, null)

update test set 
	@v = case when f1 < 5 then 1 else 0 end,
	f2 = @v*f1,
	f3 = @v * f1 * f1

select * from test

drop table test


выдает

f1	f2	f3
1 1 1
2 2 4
3 3 9
4 4 16
5 0 0
6 0 0

то, что и нужно
хотелось бы найти такое поведение в документации в явном виде

нашел только вот
https://msdn.microsoft.com/en-us/library/ms177523.aspx
expression
Is a variable, literal value, expression, or a subselect statement (enclosed with parentheses) that returns a single value. The value returned by expression replaces the existing value in column_name or @variable

и
https://msdn.microsoft.com/en-us/library/ms177523.aspx
@variable
Is a declared variable that is set to the value returned by expression.
SET @variable = column = expression sets the variable to the same value as the column. This differs from SET @variable = column, column = expression, which sets the variable to the pre-update value of the column


т.е. не понятно когда переменной присваивается значение, может по окончанию выполнения всего запроса или может сначало использоваться в выражении, а только принимать новое значение, кстати если пыполнить update, поменяв местами операторы
update test set 
	f2 = @v*f1,
	f3 = @v * f1 * f1,
	@v = case when f1 < 5 then 1 else 0 end
, то результат будет такой же

хотелось бы найти явное описание в документации, что к моменту использования переменной оно уже имеет нужное значение для текущей записи
киньте ссылку или поясните пожалуйста

Viewing all articles
Browse latest Browse all 7251

Trending Articles