Sql Cursor ....不将数据添加到临时创建的表中

时间:2013-06-13 18:42:02

标签: sql sql-server cursor

我有下面的光标。它生成正确的语句集,但我无法将数据插入临时创建的表中。光标如下。请看看。

--use a cursor to get the stats
  use qcsiteadmin_db0

-- Declare the variables needed
   DECLARE @DOMAIN_NAME VARCHAR(255)
   DECLARE @PROJECT_NAME VARCHAR(255)
   DECLARE @DB_NAME VARCHAR(60)
   DECLARE @SQL NVARCHAR(max)
   DECLARE @COUNTSQL NVARCHAR(2000)
   DECLARE @FOLDER VARCHAR(255)
   ---DECLARE @FILTER VARCHAR(500)

   select @FOLDER = 'System Validation'

   -- Create a Temp Table
  CREATE TABLE #defects(
  DOMAIN_NAME VARCHAR(255),
  PROJECT_NAME varchar(255), 
  DEFECT_NUMBER int,
  SUMMARY VARCHAR(255),
  ASSIGNED_TO VARCHAR(255),
  DESCRIPTION VARCHAR(8000),
  STATUS VARCHAR(255),
  PRIORITY int)


  --Declare the Cursor for looping the database calls
  DECLARE qc_dbs CURSOR  FOR
  select DOMAIN_NAME,PROJECT_NAME,DB_NAME
  from td.PROJECTS
  where PR_IS_ACTIVE = 'Y' 
  and DOMAIN_NAME not in
  ('mk_conversion', 'default','other','z_archivedprojects','sqa')
  order by DOMAIN_NAME,PROJECT_NAME

  --For each valid QC database get the defect counts needed
   OPEN qc_dbs
   FETCH NEXT FROM qc_dbs INTO @DOMAIN_NAME,@PROJECT_NAME,@DB_NAME
   WHILE @@FETCH_STATUS = 0
   BEGIN               


   select @SQL = 'select  ''' + @DOMAIN_NAME + ''', ''' + @PROJECT_NAME + ''', ' + @DB_NAME       + '.td.bug.BG_BUG_ID, ' + @DB_NAME + '.td.bug.BG_SUMMARY,  ' + @DB_NAME + '.td.bug.BG_RESPONSIBLE, 
                     ' + @DB_NAME + '.td.bug.BG_PRIORITY
                     from ' + @DB_NAME + '.td.CYCL_FOLD ' +
  ' INNER JOIN  ' + @DB_NAME + '.td.bug on  '
  + @DB_NAME + '.td.CYCL_FOLD.CF_ASSIGN_RCYC =  ' + @DB_NAME + '.td.BUG.BG_DETECTED_IN_RCYC 
  and ' + @DB_NAME + '.td.CYCL_FOLD.CF_ITEM_NAME = ''' + @FOLDER + ''' and ' + @DB_NAME + '.td.BUG.BG_STATUS = ''New'' '

  print @SQL

  BEGIN TRY
        insert into #defects
        execute (@SQL)
  END TRY
  BEGIN CATCH
  print 'Error accessing' + @DOMAIN_NAME + '.' + @PROJECT_NAME 
  END CATCH;


  FETCH NEXT FROM qc_dbs INTO @DOMAIN_NAME,@PROJECT_NAME,@DB_NAME
  END

  -- Close the Cursor
  CLOSE qc_dbs
  DEALLOCATE qc_dbs

  --Select from Temp Table
  select 
  DOMAIN_NAME,PROJECT_NAME,
  DEFECT_NUMBER,SUMMARY,ASSIGNED_TO,DESCRIPTION,
  STATUS, PRIORITY
  from #defects

  -- Destroy the Temp Table
  Drop TABLE #defects

我的输出消息看起来像 -

select  'ADMIN_SYSTEMS', '1099Process', admin_systems_1099process_db.td.bug.BG_BUG_ID,      admin_systems_1099process_db.td.bug.BG_SUMMARY,  admin_systems_1099process_db.td.bug.BG_RESPONSIBLE, 
                       admin_systems_1099process_db.td.bug.BG_PRIORITY
                     from admin_systems_1099process_db.td.CYCL_FOLD  INNER JOIN  admin_systems_1099process_db.td.bug on  admin_systems_1099process_db.td.CYCL_FOLD.CF_ASSIGN_RCYC =  admin_systems_1099process_db.td.BUG.BG_DETECTED_IN_RCYC 
  and admin_systems_1099process_db.td.CYCL_FOLD.CF_ITEM_NAME = 'System Validation' and admin_systems_1099process_db.td.BUG.BG_STATUS = 'New' 

 (0 row(s) affected)
 Error accessingADMIN_SYSTEMS.1099Process

请看一下。

问候。

0 个答案:

没有答案