【问题标题】:While loop in Microsoft SQL Server 2008Microsoft SQL Server 2008 中的 While 循环
【发布时间】:2010-12-25 16:56:26
【问题描述】:

我正在尝试使用 Microsoft SQL Server 提出一个简单的 while 循环示例。 在这里,我遍历所有以 temp 开头的表。

Declare @Object_ID int,@Name varchar(50)
set @Object_ID = 0
while exists (
    select * from 
    sys.tables
    where type = 'u' 
    and object_ID > @Object_ID 
    and Name like 'Temp%' 
) BEGIN
    select top 1 @Object_ID=Object_ID,@Name=Name
    from sys.tables
    where type = 'u' 
    and object_ID > @Object_ID 
    and Name like 'Temp%' 
    order by Object_ID
    exec('Select ''' + @Name + ''' as TableName,count(*) AS Counter from ' + @Name)
END

我的问题是:既然我已经遍历了表格,我该如何使用我通过 exec 命令收集的信息? 换句话说,我可以将 exec 命令返回的表存储到变量中吗?

【问题讨论】:

    标签: sql-server while-loop exec


    【解决方案1】:

    您可以存储一个临时表,类似于this example

    【讨论】:

      【解决方案2】:

      如果我正确理解你的问题,那么请看here

      只需为每次迭代插入表 var,然后在其上执行任何操作。

      【讨论】:

        【解决方案3】:

        如果您想使用动态 SQL 收集信息,那么您必须有一个地方来保存该信息 - 临时表、永久表或表变量。然后你应该可以做这样的事情:

        Declare @Object_ID int,@Name varchar(50)
        DECLARE
            @tbl    TABLE (table_name SYSNAME, table_count INT)
        
        set @Object_ID = 0
        while exists (
            select * from 
            sys.tables
            where type = 'u' 
            and object_ID > @Object_ID 
            and Name like 'Temp%' 
        ) BEGIN
            select top 1 @Object_ID=Object_ID,@Name=Name
            from sys.tables
            where type = 'u' 
            and object_ID > @Object_ID 
            and Name like 'Temp%' 
            order by Object_ID
        
            INSERT INTO @tbl (table_name, table_count)
            exec('Select ''' + @Name + ''' as TableName,count(*) AS Counter from ' + @Name)
        END
        
        SELECT * FROM @tbl
        

        【讨论】:

        • 哦,我没想到在 exec 前面做一个插入!谢谢!
        【解决方案4】:

        这些天我是这样做这种任务的:

        DECLARE
          @LoopId     int
         ,@Command    varchar(500)
        
        DECLARE @List TABLE
         (
           TableName  sysname  not null
          ,LoopId     int      not null  identity(1,1)
         )
        
        DECLARE @Results TABLE
         (
           TableName  sysname  not null
          ,Counter    int      not null
         )
        
        --  Load with items you wish to review
        INSERT @List (TableName)
         select name
          from sys.tables
          where Name like 'Temp%'
          order by Name desc
        
        SET @LoopId = @@rowcount
        
        --  Go through list and process each item
        WHILE @LoopId > 0
         BEGIN
            SELECT @Command = 'SELECT ''' + TableName + ''' as TableName, count(*) as Counter from ' + TableName
             from @List
             where LoopId = @LoopId
        
            --  Load results in temp table
            INSERT @Results (TableName, Counter)
             execute (@Command)
        
            SET @LoopId = @LoopId - 1
         END
        
        SELECT * from @Results
        

        【讨论】:

        • 这是一个很好的回答菲利普,谢谢!我所有的 SQL 都属于你。
        猜你喜欢
        • 2018-10-28
        • 2014-02-10
        • 2011-05-28
        • 2017-08-06
        • 2013-08-18
        • 2015-12-30
        • 2014-11-10
        • 2020-03-01
        • 2014-03-11
        相关资源
        最近更新 更多