江西广告网

标题: 获取 SQL SERVER用户表信息游标 [打印本页]

作者: 印度阿三    时间: 2009-1-20 09:22
标题: 获取 SQL SERVER用户表信息游标
  /*获取用户表信息光标*/   SET NOCOUNT ON   DECLARE find_user_table CURSOR   FOR   SELECT [name],crdate FROM sysobjects   WHERE type='U' /*这里只查询用户定义的表*/   ORDER BY [name]   DECLARE @cName VARCHAR(128) /*定义储存表名变量*/   DECLARE @crdate DATETIME /*定义表创建日期变量*/   DECLARE @Rows INT /*定义表行数变量*/   CREATE TABLE #tmpTable /*创建用来储存信息的临时表*/   (   Tablename VARCHAR(128),   crDate datetime,   Row INT   )   OPEN find_user_table /*打开游标*/   FETCH NEXT FROM find_user_table INTO @cName,@crdate /*从光标中读取表名到变量*/   WHILE @@FETCH_STATUS=0   BEGIN    DECLARE @cSql nvarchar(500),@par nvarchar(30)    SELECT @par='@nRows INT OUTPUT'    SELECT @cSql='SELECT @nRows=COUNT(*) FROM [' @cName ']'    EXECUTE sp_executesql @cSql,@par,@rows OUTPUT /*计算当前表的总行数*/    INSERT INTO #tmpTable valueS(@cName,@crdate,@rows) /*将当前表信息存储到临时表*/    FETCH NEXT FROM find_user_table INTO @cName,@crdate /*从光标中读取表名到变量*/    END   SELECT * FROM #tmpTable /*显示所有表信息*/   DROP TABLE #tmpTable /*删除临时表*/   CLOSE find_user_table /*关闭游标*/   DEALLOCATE find_user_table /*释放游标*/       <




欢迎光临 江西广告网 (http://bbs.jxadw.com/) Powered by Discuz! X3.2