我正在尝试为页面压缩构建动态查询,但在执行时它显示错误。
alter PROCEDURE usp_FindTableNameInAllDatabase
@dbName VARCHAR(256)
AS
DECLARE @tableName VARCHAR(256)
DECLARE @varSQL VARCHAR(512)
DECLARE @gettableName CURSOR
SET @gettableName = CURSOR FOR
SELECT tablename
FROM table_list
OPEN @gettableName
FETCH NEXT
FROM @gettableName INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @varSQL = 'USE ' + @DBName + '
ALTER TABLE '+@tableName+' REBUILD PARTITION = ALL
WITH
(DATA_COMPRESSION = PAGE
)'
exec (@varSQL)
FETCH NEXT
FROM @gettableName INTO @tableName
END
CLOSE @gettableName
DEALLOCATE @gettableName
GO
EXEC usp_FindTableNameInAllDatabase '[AdventureWorks2014]'
GO
哪个是错误的:
Ms 16943,Level 16,State 4,Procedure usp_FindTableNameInAllDatabase,第50行
无法完成游标操作,因为表架构已更改 声明游标后。
如何解决这个问题?
答案 0 :(得分:0)
DECLARE @PageSize INT,
@PageNumber INT,
@FirstRow INT,
@LastRow INT
SELECT @PageSize = 20,
@PageNumber = 1
SELECT @FirstRow = ( @PageNumber - 1) * @PageSize + 1,
@LastRow = (@PageNumber - 1) * @PageSize + @PageSize ;
WITH Members AS
(
SELECT Col1,Col2,Col3,
ROW_NUMBER() OVER (ORDER BY Col1 DESC) AS RowNumber
FROM Table
)
SELECT Col1,Col2,..,..,..,
FROM Members
WHERE RowNumber BETWEEN @FirstRow AND @LastRow
ORDER BY Col1 ASC;
使用它。这可能对你有所帮助。 :)
答案 1 :(得分:0)
如果该表由游标处理,则无法更改表的架构或结构。
在查询中,当该表的光标处于活动状态时,您尝试更改表的结构。
如果要更改表结构,只需在声明游标或解除分配游标之后再执行此操作。