我糊涂了.你如何用ORDER BY解释变量连接中的这种差异?
declare @tbl table (id int);
insert into @tbl values (1), (2), (3);
declare @msg1 varchar(100) = '', @msg2 varchar(100) = '',
@msg3 varchar(100) = '', @msg4 varchar(100) = '';
select @msg1 = @msg1 + cast(id as varchar) from @tbl
order by id;
select @msg2 = @msg2 + cast(id as varchar) from @tbl
order by id+id;
select @msg3 = @msg3 + cast(id as varchar) from @tbl
order by id+id desc;
select TOP(100) @msg4 = @msg4 + cast(id as varchar) …
Run Code Online (Sandbox Code Playgroud) 如何获取列中的下一个非空值?我有 MSSQL 2012 和只有一列的表。像这样:
rownum Orig
------ ----
1 NULL
2 NULL
3 9
4 NULL
5 7
6 4
7 NULL
8 9
Run Code Online (Sandbox Code Playgroud)
我需要这些数据:
Rownum Orig New
------ ---- ----
1 NULL 9
2 NULL 9
3 9 9
4 NULL 7
5 7 7
6 4 4
7 NULL 5
8 9 5
Run Code Online (Sandbox Code Playgroud)
启动代码:
declare @t table (rownum int, orig int);
insert into @t values (1,NULL),(2,NULL),(3,9),(4,NULL),(5,7),(6,4),(7,NULL),(8,9);
select rownum, orig from @t;
Run Code Online (Sandbox Code Playgroud)