很多时候,我们经常使用sp_spaceused来查看表的空间使用情况,上个月群里有个网友说他使用DELETE删除了数据后,使用sp_spaceused查看,发现该表的分配的空间总量(reserved)与数据使用的空间总量(data)没有变化,当时和他讨论了并分析了一下原因,随手记录了一下这个案例,这个周末刚好有点时间,正好分析整理一下这个案例.分享在这篇文章.如下所示,我们先构造数据,我们的测试案例比较极端,刚刚保证每个页面(page)刚好存储两条记录.如下所示: USE Test ; GO …
SQL Server 没有类似于Oracle START WITH NAME='xx' CONNECT BY PRIOR ID=PARENT_ID这样的语句,但是可以通过自定义标准函数+With语句实现,速度也是杠杠的 ALTER FUNCTION [dbo].[RecursionSysLocation] ( -- Add the parameters for the function here ) ) RETURNS TABLE AS RETURN ( with temp ( [Id], [p…
每个地区递归层级可能不一致,数据表(table)存放最小层级地区 area --地区层级表 id name f_id leve 1 中国 0 1 2 湖北 1 2 3 武汉 2 3 ... --测试数据 with area(id,"name",f_id,leve) as ( ,, union all ,, union all ,, union all ,, union all ,, union all ,, union all ,, union all ,, union all ,,…
with cte as ( select belongsAgent from [QPProxyDB].[dbo].[BS_ProxyInfo] where ProxyID = @ProxyID union all select a.ProxyID from [QPProxyDB].[dbo].[BS_ProxyInfo] a join cte b on a.ProxyID = b.belongsAgent ) select * from cte order by belongsAgent asc…
@EmptyCount int output 参数 declare @strCount nvarchar(max); 声明变量 取值语句: set @strCount='select @Count=count(1) from testtable AS B WHERE B.Status=0 '; 执行命令语句 EXECUTE SP_EXECUTESQL @strSQL ; EXECUTE SP_EXECUTESQL @strCount,N'@Count int output',@EmptyCou…