本文发表在 rolia.net 枫下论坛-- =============================================
-- Author : DiGiTwoRM
-- Create date: Feb 8, 2007
-- Description: Get row count of a table
-- @table_name - table name as an input parameter
-- @count - count number an output parameter
-- =============================================
ALTER PROCEDURE [dbo].[TableCount]
@table_name varchar(128),
@count int out
AS
BEGIN
SET NOCOUNT Off;
DECLARE @sql nvarchar(1000)
DECLARE @parameters nvarchar(1000)
SET @table_name = 'customers'
SET @count = 0
SET @sql = 'SELECT @count = count(*) FROM dbo.' + quotename(@table_name)
SET @parameters = '@table_name varchar(128), @count int out'
EXEC sp_executesql @sql, @parameters, @table_name, @count out
END
-----------------------------------------------
I tested it with northwind database
-----------------------------------------------
use northwind
go
declare @t varchar(100)
declare @n int
set @t= 'customers'
exec tablecount @t, @n out
print @n
go
-----------------------------------------------
Result
-----------------------------------------------
91更多精彩文章及讨论,请光临枫下论坛 rolia.net
-- Author : DiGiTwoRM
-- Create date: Feb 8, 2007
-- Description: Get row count of a table
-- @table_name - table name as an input parameter
-- @count - count number an output parameter
-- =============================================
ALTER PROCEDURE [dbo].[TableCount]
@table_name varchar(128),
@count int out
AS
BEGIN
SET NOCOUNT Off;
DECLARE @sql nvarchar(1000)
DECLARE @parameters nvarchar(1000)
SET @table_name = 'customers'
SET @count = 0
SET @sql = 'SELECT @count = count(*) FROM dbo.' + quotename(@table_name)
SET @parameters = '@table_name varchar(128), @count int out'
EXEC sp_executesql @sql, @parameters, @table_name, @count out
END
-----------------------------------------------
I tested it with northwind database
-----------------------------------------------
use northwind
go
declare @t varchar(100)
declare @n int
set @t= 'customers'
exec tablecount @t, @n out
print @n
go
-----------------------------------------------
Result
-----------------------------------------------
91更多精彩文章及讨论,请光临枫下论坛 rolia.net