效率比较高sql语句。
IFEXISTS(SELECTnameFROMdbo.sysobjectsWHEREid=Object_id(N'[dbo].[row_count]'))
DROPFUNCTION[dbo].[row_count]
GO
CREATEFUNCTIONdbo.row_count(@table_namesysname)
--@table_namewewanttogetcount
RETURNSbigint
AS
BEGIN
DECLARE@nnbigint--numberofrows
IF@table_nameISNOTNULL
BEGIN
SELECT@nn=sum(p.rows)
FROMsys.partitionsp
LEFTJOINsys.allocation_unitsaONp.partition_id=a.container_id
WHERE
p.index_idin(0,1)
andp.rowsisnotnull
anda.type=1
andp.object_id=object_id(@table_name)
END
RETURN(@nn)
END
DROPFUNCTION[dbo].[row_count]
GO
CREATEFUNCTIONdbo.row_count(@table_namesysname)
--@table_namewewanttogetcount
RETURNSbigint
AS
BEGIN
DECLARE@nnbigint--numberofrows
IF@table_nameISNOTNULL
BEGIN
SELECT@nn=sum(p.rows)
FROMsys.partitionsp
LEFTJOINsys.allocation_unitsaONp.partition_id=a.container_id
WHERE
p.index_idin(0,1)
andp.rowsisnotnull
anda.type=1
andp.object_id=object_id(@table_name)
END
RETURN(@nn)
END
GO
比count(*)效率高很多。