我有一个临时表,它散发出一堆行,如下所示:
临时表结构:
create table #final(rownum int,_CID int,Output nvarchar(max))
insert into #final
select rownum,_CID ,Output from ...and goes the sproc
最终选择:
declare @num_records int = @@ROWCOUNT
if @num_records > 0
select Output from #final
所以输出是:
12345abc 65432 TEST1 2383 JOHN KIARA N 987456321456
12345abc 65432 TEST2 3570 JANE lastname A 989746532366
12345abc 65432 TEST3 3573 KANE AMBER N 987454121445
要求:我想在输出中添加最后一行,以显示上面的记录数。
所需的输出:
12345abc 65432 TEST1 2383 JOHN KIARA N 987456321456
12345abc 65432 TEST2 3570 JANE lastname A 989746532366
12345abc 65432 TEST3 3573 KANE AMBER N 987454121445
TRAILER 3
有没有办法做到这一点?我将尽快添加一个rextester链接。有帮助吗?!
答案 0 :(得分:1)
选择行之后可以使用UNION
,这会将行添加到SELECT
语句返回的结果集的末尾不在最后一行之后没有订单,没有最后一行:
SELECT *
FROM #final
UNION
SELECT
'TRAILER', (SELECT COUNT(1) FROM #final), NULL,NULL,NULL,NULL,NULL,NULL;
结果:
+----------+-------+-------+------+------+----------+------+--------------+
| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8 |
+----------+-------+-------+------+------+----------+------+--------------+
| 12345abc | 65432 | TEST1 | 2383 | JOHN | KIARA | N | 987456321456 |
| 12345abc | 65432 | TEST2 | 3570 | JANE | lastname | A | 989746532366 |
| 12345abc | 65432 | TEST3 | 3573 | KANE | AMBER | N | 987454121445 |
| TRAILER | 3 | NULL | NULL | NULL | NULL | NULL | NULL |
+----------+-------+-------+------+------+----------+------+--------------+