3
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
4

4 に答える 4

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 に答える
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

http://blog.sqlauthority.com/2010/09/08/sql-server-find-row-count-in-table-find-largest-table-in-database-part-2/を参照してください。

于 2012-09-05T14:03:41.487 に答える
2

FROMTABLEではなく読んでいたFROMキーワードの後に​​スペースがなかったようですFROM Table

DECLARE @TABLE NVARCHAR(100)
SET @TABLE = 'CLIENT' -- YOUR TABLE NAME

EXEC('SELECT COUNT(*) FROM ' + @TABLE)
于 2012-09-05T13:52:30.540 に答える