1
我創建了一個實現的COUNT(*)
操作的更快的替代方案SQL函數:行數函數總是返回NULL
create function fast_row_count (@table_name varchar)
returns bigint
as
begin
declare @row_count int;
set @row_count =
(select sum(row_count) from sys.dm_db_partition_stats with (nolock) where object_id = object_id(@table_name));
return @row_count
end
go
執行時,它總是返回NULL值。
select dbo.fast_row_count('tbl_calls')
然而,作爲一個單獨的批次中的硬編碼值執行時,它工作正常:
declare @row_count int;
set @row_count =
(select sum(row_count) from sys.dm_db_partition_stats with (nolock) where object_id = object_id('tbl_calls'));
print @row_count
'varchar'沒有長度?這是一個禁忌。嘗試'SYSNAME'。 – 2015-02-24 09:31:41
'varchar'沒有長度就是'varchar(1)'。你的表名可能不適合一個字符,是嗎? – Luaan 2015-02-24 09:32:38
[不良習慣踢:聲明VARCHAR沒有(長度)](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length .aspx) – GarethD 2015-02-24 09:36:11