在sql存储过程中循环并删除游标

时间:2015-05-26 23:25:17

标签: sql sql-server stored-procedures

我必须修改由其他人编写的STORED过程。基本上,存储的prcoedure使用cusrsor从表中获取数据,然后将该数据插入另一个表中。从另一个表中获取代码时,它也会从另一个表中获取一些不同的列。下面是我的代码:

 Declare data_cursor cursor for

    Select emp_no, emp_name, event_date, Test_no, Code, Test_result
      From test_table1
     ORDER by emp_no

declare
   @empNo   varchar(100),
   @emp_name varchar(2000),
   @eventDate  varchar(20),
   @TestNo     varchar(100),
   @Code       varchar(100),
   @TestReuslt  varchar(100),
   @ProcessName  varchar(100),
   @FileProcess   varchar(200),
   @TestProcess   varchar(100),
   @countA         int,
   @error_count int

SELECT @ProcessName = (select distinct userID from test_table1)
SELECT @FileProcess = 'EW' + @ProcessName 
Select @TestProcess  = (Select distinct userID from test_Table1) + 'TXT'
select @countA = 0

  BEGIN tran
     OPEN data_cursor  
     fetch data_cursor into
      @empNo  ,
      @emp_name ,
      @eventDate  ,
      @TestNo  ,  
      @Code      , 
      @TestReuslt
  while (@@FETCH_STATUS=0)
     begin
      insert into TESTTable2
            (
               empNum, empName, eventDate,TestNum, Code, TestResult, Testprocess,  ProcessName)
       values (@empNo, @emp_name, @eventDate , @TestNo , @Code, @TestReuslt, @TestProcess, @ProcessName)
   if @ ERROR > 0
            begin
                select @error_count = @error_count + 1
            end

        else
            set @record_id = @@Identity

        if @code like 'D%'
                  Insert into TESTTable3
            (testProcess, FileProcess, empNum)
                 values (@TestProcess,  @FileProcess, @empNo )
    if @@error > 0
            begin
                select @error_count = @error_count + 1
            end 

         set @countA = @countA + 1
       fetch data_cursor into
         fetch data_cursor into
      @empNo  ,
      @emp_name ,
      @eventDate  ,
      @TestNo  ,  
      @Code      , 
      @TestReuslt
if @ ERROR > 0
    BEGIN
        select @error_count = @error_count + 1
        end
    end 

     if @error_count > 0 
    begin
    rollback tran
    end
else

    begin /* @@error = 0 */
    commit tran

close data_cursor
deallocate data_cursor


     Insert into LOG_File
      (Name, Count, Processname)
       values ('Test1', @CountA,@ProcessName)

Select 'TotalCount' = @CountA

原因,我现在必须修改上面的STORED proc是因为一些APPLICATION更改,我从test_table1得到大约50个不同的userID,所以上面的子查询(SELECT @ProcessName =(从test_table1中选择不同的userID)不如何循环上面存储的proc,以便每个@ProcessName可以插入到表TESTTable2中,换句话说

我想一次传递一个userId,并将其插入表test_table1和其他后续表中。我可以声明另一个光标来实现这一点,但我想知道是否有更好的方法来重写这个存储过程并且根本不使用光标。

由于我的应用程序更改,上述所有这三个语句都抛出错误:

SELECT @ProcessName = (select distinct userID from test_table1)
SELECT @FileProcess = 'EW' + @ProcessName 
Select @TestProcess  = (Select distinct userID from testTable1) + 'TXT'

我正在使用sql server 2005。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

declare @countA int=0

begin tran
begin try
    insert into TESTTable2(empNum, empName, eventDate,TestNum, Code, TestResult, Testprocess,  ProcessName)
        Select emp_no, emp_name, event_date, Test_no, Code, Test_result,userID+ 'TXT',userID
        From test_table1
        ORDER by emp_no

    SET @CountA=@@ROWCOUNT

    Insert into TESTTable3(testProcess, FileProcess, empNum)
        Select userID+ 'TXT','EW' + userID,emp_no
        From test_table1
        Where code like 'D%'
        ORDER by emp_no

    commit tran

    Insert into LOG_File(Name, Count, Processname) values ('Test1', @CountA,'@ProcessName')
end try
begin catch
    rollback tran

    SET @CountA =0
    Insert into LOG_File(Name, Count, Processname) values ('Test1', @CountA,'@ProcessName')

    SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage
end catch

Select @CountA [TotalCount]