nvarchar(max) 与 order by语句问题
测试数据:
CREATE TABLE Project1
(
ID int primary key identity(1,1) not null,
Name nvarchar(50),
Code nvarchar(20),
Value int,
Details nvarchar(max),
IsAvailable bit
)
INSERT INTO Project1 VALUES('Case1','C1','100','Case1 details',1)
INSERT INTO Project1 VALUES('Case2','C2','200','Case2 details',1)
INSERT INTO Project1 VALUES('Case3','C3','100','Case3 details',0)
INSERT INTO Project1 VALUES('Case4','C4','300','Case4 details',1)
INSERT INTO Project1 VALUES('Case5','C5','400','Case5 details',1)
查询语句:
DECLARE @a nvarchar(max) = ''
SELECT
@a = @a + details
FROM Project1
WHERE IsAvailable = 1
ORDER BY Value
SELECT @a
结果: Case5 details
这里如果不加order by的结果:Case1 detailsCase2 detailsCase4 detailsCase5 details
我试过order by换成ID,IsAvailable都是可以的,其他却都不行,很疑惑。
实际项目中就是需要类型为nvarchar(max)的字段累加,并且通过order by一些字段。
请大侠们给点意见啊,搞了老半天了!~~~~ SQLNVARCHAR?MAX
[解决办法]
DECLARE @a nvarchar(max) = ''
SELECT
@a = @a + T.details
FROM (SELECT TOP 100 percent * FROM Project1 ORDER BY Value)T --这样呢
WHERE T.IsAvailable = 1
SELECT @a