У меня вопрос, почему выходные данные этих двух запросов различаются.Я ожидал бы, что они будут работать одинаково.
Запрос 1:
declare @cache table(originalValue nvarchar(255), obfuscateValue nvarchar(255));
declare @table1 table(c char(1));
declare @i1 int;
set @i1 = ASCII('0');
while @i1 <= ASCII('9')
begin
insert into @table1 (c)
select (CHAR(@i1))
set @i1 = @i1 +1;
end
insert into @cache (originalValue, obfuscateValue)
select [firstname],
(select top 1 c from @table1 order by NEWID()) +
(select top 1 c from @table1 order by NEWID())
from Customer
where [firstname] is not null
select * from @cache;
Запрос 2:
declare @cache table(originalValue nvarchar(255), obfuscateValue nvarchar(255));
declare @table1 table(c char(1));
declare @i1 int;
set @i1 = ASCII('0');
while @i1 <= ASCII('9')
begin
insert into @table1 (c)
select (CHAR(@i1))
set @i1 = @i1 +1;
end
insert into @cache (originalValue)
select [firstname]
from Customer
where [firstname] is not null
update c
set c.obfuscateValue = t.Value
from @cache c
join
(
select originalValue,
(
(select top 1 c from @table1 order by NEWID()) +
(select top 1 c from @table1 order by NEWID())
) as Value
from @cache
) t on t.originalValue = c.originalValue
select * from @cache;
Они должны делать то же самое, но первый запрос возвращает следующие результаты:
Jonathon 73
Everett 73
Janet 73
Andy 73
Shauna 73
И второй:
Jonathon 82
Everett 40
Janet 68
Andy 79
Shauna 29
Как вы заметили, второй столбец во втором результате имеет разные значения,а first - те же значения.
Похоже, что в первом запросе
(select top 1 c from @table1 order by NEWID()) +
(select top 1 c from @table1 order by NEWID())
вызывается только один раз.
Может кто-нибудь объяснить эту тайну?