Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
全テーブルの件数表示 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
You can’t perform that action at this time.