sqlserver存储过程何循环读表。

发布网友 发布时间:2022-04-20 00:43

我来回答

1个回答

热心网友 时间:2022-04-11 16:56

使用游标,如下
CREATE PROCEDURE proc_getalltable
AS
BEGIN
SET NOCOUNT ON;

DECLARE @tablename VARCHAR(100),@sql VARCHAR(1000)

DECLARE tablename CURSOR FOR SELECT tname FROM tablelist

OPEN tablename
FETCH NEXT FROM tablename INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql = 'SELECT * FROM ' + @tablename
EXEC(@sql)
FETCH NEXT FROM tablename INTO @tablename
END

CLOSE tablename
DEALLOCATE tablename
END
GO

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com