Skip to content

Instantly share code, notes, and snippets.

@okazbb
Last active December 26, 2015 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okazbb/7191794 to your computer and use it in GitHub Desktop.
Save okazbb/7191794 to your computer and use it in GitHub Desktop.
全テーブルの件数表示 for SQLServer7.0
DECLARE @TABLE_NAME NVARCHAR(100)
DECLARE @SQL NVARCHAR(100)
DECLARE @ROW_COUNT int
CREATE TABLE #T_RESULT(
TABLE_NAME NVARCHAR(100),
ROW_COUNT int
)
DECLARE C_TABLES CURSOR FOR
SELECT name
FROM sysobjects
WHERE (type = 'U')
OPEN C_TABLES
FETCH NEXT FROM C_TABLES INTO @TABLE_NAME
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = N'SELECT @ROW_COUNT = COUNT(*) FROM ' + @TABLE_NAME
EXEC SP_EXECUTESQL @SQL, N'@ROW_COUNT int OUTPUT', @ROW_COUNT OUTPUT
INSERT INTO #T_RESULT VALUES (@TABLE_NAME, @ROW_COUNT)
FETCH NEXT FROM C_TABLES INTO @TABLE_NAME
END
CLOSE C_TABLES
DEALLOCATE C_TABLES
SELECT * FROM #T_RESULT ORDER BY TABLE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment