nvarchar串联/索引/nvarchar(Max)令人费解的行为
select
-- base tablecreate table bla ( [id] int identity(1,1) primary key, [priority] int, [msg] nvarchar(max), [autofix] bit)-- table without primary key on id columncreate table bla2 ( [id] int identity(1,1), [priority] int, [msg] nvarchar(max), [autofix] bit)-- table with nvarchar(1000) instead of maxcreate table bla3 ( [id] int identity(1,1) primary key, [priority] int, [msg] nvarchar(1000), [autofix] bit)-- fill the three tables with the same valuesinsert into bla ([priority], [msg], [autofix])values (1, 'A', 0), (2, 'B', 0)insert into bla2 ([priority], [msg], [autofix])values (1, 'A', 0), (2, 'B', 0)insert into bla3 ([priority], [msg], [autofix])values (1, 'A', 0), (2, 'B', 0);declare @a nvarchar(max) = ''declare @b nvarchar(max) = ''declare @c nvarchar(max) = ''declare @d nvarchar(max) = ''declare @e nvarchar(max) = ''declare @f nvarchar(max) = ''-- I expect this to work and generate 'AB', but it doesn'tselect @a = @a + [msg] from bla where autofix = 0 order by [priority] asc-- this DOES work: convert nvarchar(4000)select @b = @b + convert(nvarchar(4000),[msg]) from bla where autofix = 0 order by [priority] asc-- this DOES work: without WHERE clauseselect @c = @c + [msg] from bla --where autofix = 0 order by [priority] asc-- this DOES work: without the order byselect @d = @d + [msg] from bla where autofix = 0 --order by [priority] asc-- this DOES work: from bla2, so without the primary key on idselect @e = @e + [msg]
绝地无双
相关分类