create procedure qa_cc
@tablename varchar(500)
AS
BEGIN
-- Create two integer values
DECLARE @tableOneCount varchar(50), @tableTwoCount varchar(50)
-- Get the number of rows from the first table
SELECT @tableOneCount = 'SELECT COUNT(*) FROM' + @tablename;
exec (@tableOneCount);
select @tableOneCount ;
END
exec qa_cc @tablename=table1
问问题
8704 次
4 回答
3
SELECT SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
and ta.name=@tablename
GROUP BY ta.name
于 2012-09-05T14:03:41.487 回答
3
您可以使用sp_executesql
,例如:
declare @sql nvarchar(max);
set @sql = N'select @cnt = count(*) from ' + @tablename;
declare @cnt int;
exec sp_executesql @sql, N'@cnt int output', @cnt = @cnt output;
select @cnt;
于 2012-09-05T13:52:27.647 回答
2
看起来你在 FROM 关键字后面缺少一个空格,它正在阅读FROMTABLE
而不是FROM Table
DECLARE @TABLE NVARCHAR(100)
SET @TABLE = 'CLIENT' -- YOUR TABLE NAME
EXEC('SELECT COUNT(*) FROM ' + @TABLE)
于 2012-09-05T13:52:30.540 回答
-1
CREATE PROCEDURE [dbo].[TableRowCount]
@tableName text
AS
EXEC ('SELECT COUNT(*) FROM ' + @tableName)
RETURN @@ROWCOUNT
我希望这会有所帮助。谢谢你。
于 2014-05-15T22:09:01.050 回答