DECLARE @tableName VARCHAR ( 30 ), @selestr NVARCHAR ( 100 )
DECLARE @i int , @total INT
DECLARE ourcur CURSOR FOR SELECT name FROM sysobjects WHERE xtype = ' U '
OPEN ourcur
FETCH NEXT FROM ourcur INTO @tableName
SET @total = 0
WHILE @@FETCH_STATUS = 0
BEGIN
IF @tableName LIKE statemen_1 -- 符合第一个条件的表
BEGIN
SET @selestr = ' SELECT @m=COUNT(*) FROM ' + @tableName
EXEC sp_executesql @selestr ,N ' @m INT OUTPUT ' , @i OUTPUT
SET @total = @total + @i
END
ELSE IF @tableName LIKE statement_2 -- 符合第二个条件的表
BEGIN
SET @selestr = ' SELECT @m=COUNT(*) FROM ' + @tableName
EXEC sp_executesql @selestr ,N ' @m INT OUTPUT ' , @i OUTPUT
SET @total = @total + @i
END
ELSE IF @tableName LIKE statement_3 -- 符合第三个条件的表
BEGIN
SET @selestr = ' SELECT @m=COUNT(*) FROM ' + @tableName
EXEC sp_executesql @selestr ,N ' @m INT OUTPUT ' , @i OUTPUT
SET @total = @total + @i
END
FETCH NEXT FROM ourcur INTO @tableName
END
DEALLOCATE ourcur
SELECT @total AS TotalDataNum -- 显示记录总数
DECLARE @i int , @total INT
DECLARE ourcur CURSOR FOR SELECT name FROM sysobjects WHERE xtype = ' U '
OPEN ourcur
FETCH NEXT FROM ourcur INTO @tableName
SET @total = 0
WHILE @@FETCH_STATUS = 0
BEGIN
IF @tableName LIKE statemen_1 -- 符合第一个条件的表
BEGIN
SET @selestr = ' SELECT @m=COUNT(*) FROM ' + @tableName
EXEC sp_executesql @selestr ,N ' @m INT OUTPUT ' , @i OUTPUT
SET @total = @total + @i
END
ELSE IF @tableName LIKE statement_2 -- 符合第二个条件的表
BEGIN
SET @selestr = ' SELECT @m=COUNT(*) FROM ' + @tableName
EXEC sp_executesql @selestr ,N ' @m INT OUTPUT ' , @i OUTPUT
SET @total = @total + @i
END
ELSE IF @tableName LIKE statement_3 -- 符合第三个条件的表
BEGIN
SET @selestr = ' SELECT @m=COUNT(*) FROM ' + @tableName
EXEC sp_executesql @selestr ,N ' @m INT OUTPUT ' , @i OUTPUT
SET @total = @total + @i
END
FETCH NEXT FROM ourcur INTO @tableName
END
DEALLOCATE ourcur
SELECT @total AS TotalDataNum -- 显示记录总数
statement_1/_2/_3…可以插入多个条件来进行匹配操作,期货总的sql语句也可以替换成插入、更新等,批量处理,很方便!