动态SQL的SQL Server范围问题

时间:2016-09-07 19:32:52

标签: sql sql-server dynamic

以下代码来自我们的ERP系统:

ALTER procedure [dbo].[spGrantAFUserSelectPerm] 
iLoginName     varchar(255) = null,    
@iAppDB         varchar(255)= null,     
@iPLDB          varchar(255)= null,      
@retcode        int output                 

as 
   declare @TableName      varchar(255) 
   declare @TableCursor    varchar(255) 
   declare @ProcName       varchar(255) 
   declare @ProcCursor     varchar(255) 

   select @retcode = 1 

   if @iLoginName is null
           return @retcode 

   --AppDB 
   if @iAppDB is not null and @iAppDB <> ''
   begin
       select @TableCursor = 'select name from ' + @iAppDB + '..sysobjects where (type = ''U'' OR type = ''V'' OR type = ''TF'') AND uid = 1' 
       exec ('declare c_GrantAppTableAccess cursor for ' + @TableCursor + ' for READ ONLY') 
       open c_GrantAppTableAccess       
       fetch c_GrantAppTableAccess into @TableName 
       while @@fetch_status = 0 
       begin 
           exec ('use ' + @iAppDB + ' grant SELECT on ' + @TableName + ' to [' + @iLoginName + ']') 
               fetch c_GrantAppTableAccess into @TableName 
       end      
       close c_GrantAppTableAccess 
       deallocate c_GrantAppTableAccess 
   end

这段代码工作多年,直到上周,当时我们的一位开发人员尝试(不成功)部署CLR sproc。现在,sproc失败了#34;打开c_GrantAppTableAccess&#34;有错误说光标不存在。就好像游标声明超出了范围。有任何想法吗?再次,这在上周之前工作了5到7年。

0 个答案:

没有答案