if exists (select 1 where OBJECT_ID('tempdb..#len') is not null) drop table #len 
select distinct IDENTITY (int,1,1) id, len(ccode) ilen ,CONVERT(varchar(255),'') as jc
 into #len from @t6db..code order by ilen
 update a set a.jc=a.ilen -ISNULL(b.ilen,0) from 
	#len a left join #len b on a.id =b.id +1
	 

declare @jc varchar(300)
declare @jc2 varchar(500)
set @jc=''
set @jc2=''
select @jc=@jc+CONVERT(varchar(10),jc),
		@jc2=@jc2+CONVERT(varchar(10),jc)+','
from #len order by ilen 
if @jc2<>''
set @jc2=SUBSTRING (@jc2,1,len(@jc2)-1)
select @jc,@jc2
if exists (select * from @t6db..gradedef where KEYWORD='code' and len(CODINGRULE)<len(@jc))
	
	update @t6db..gradedef set CODINGRULE =@jc where KEYWORD='code'
if exists (select * from @t6db..AccInformation where cSysID ='AA' and cName ='cGradeLevel' 
		and len(cvalue)<len(@jc2))
begin
	update @t6db..AccInformation set cvalue =@jc2 where cSysID ='AA' and cName ='cGradeLevel'
	update @u8db..AccInformation set cvalue =@jc2 where cSysID ='AA' and cName ='cGradeLevel'
end
if exists (select 1 where OBJECT_ID('tempdb..#len') is not null) drop table #len 
go
--------科目扩位begin----------
use @u8db
go
------------------总账begin---------------------------------


if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @constname varchar(300), @tblname varchar(200), @cur cursor,@sql as nvarchar(2000)
	set @cur = cursor for 
			 select o.name as tablename,o2.name as constname from sysforeignkeys s
			 left join sysobjects  o on fkeyid=o.id 
			 left join sysobjects o2 on constid=o2.id  and o2.parent_obj=fkeyid
			 where rkeyid=OBJECT_ID(N'code') 
	open @cur 
	fetch next from @cur into @tblname,@constname
	while @@fetch_status = 0 
	begin 
			 set @sql='IF  EXISTS (SELECT * FROM sysobjects 
			 WHERE id = OBJECT_ID(N'''+@constname+''') AND  type = ''F'')
			 alter table '+@tblname+' drop constraint '+@constname 
	print @sql 
	exec (@sql)
	fetch next from @cur into @tblname,@constname 
	end 
	close @cur 
	deallocate @cur


	if exists(select * from sysindexes 
	where id=object_id(N'code') and name='aaaaacode_PK')
	alter table code drop constraint aaaaacode_PK
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	alter table code alter column ccode nvarchar(40)  not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	alter table code add constraint aaaaacode_PK PRIMARY KEY NONCLUSTERED 
	(    [ccode] ASC
	) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	if exists (select * from dbo.sysindexes where name = N'ccode' and id = object_id(N'[dbo].[gl_accvouch]'))
	drop index [gl_accvouch].[ccode]
end
GO

--科目编码增加长度
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	alter table gl_accvouch alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	if exists(select * from sysindexes 
	where id=object_id(N'GL_accsum') and name='aaaaaGL_accsum_PK')
	alter table GL_accsum drop constraint aaaaaGL_accsum_PK
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF  EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[dbo].[GL_accsum]') AND name = N'period')
DROP INDEX [GL_accsum].[period]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table gl_accsum alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_accsum add constraint aaaaaGL_accsum_PK primary key(ccode, iperiod)
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE UNIQUE NONCLUSTERED INDEX [period] ON [GL_accsum] 
(        [iperiod] ASC,
         [ccode] ASC
) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode')
drop index gl_accass.ccode
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_cus')
drop index gl_accass.ccode_cus
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_cus_dept')
drop index gl_accass.ccode_cus_dept 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_cus_item')
drop index gl_accass.ccode_cus_item 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_dept')
drop index gl_accass.ccode_dept 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_item')
drop index gl_accass.ccode_item 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_item_dept')
drop index gl_accass.ccode_item_dept 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_person')
drop index gl_accass.ccode_person 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_post')
drop index gl_accass.ccode_post 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_sup')
drop index gl_accass.ccode_sup 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_sup_dept')
drop index gl_accass.ccode_sup_dept 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_accass') and name='ccode_sup_item')
drop index gl_accass.ccode_sup_item 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table gl_accass alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [ccode] ON [gl_accass](ccode, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_cus] ON [gl_accass](ccode, ccus_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_cus_dept] ON [gl_accass](ccode, ccus_id, cdept_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_cus_item] ON [gl_accass](ccode, ccus_id, citem_class, citem_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_dept] ON [gl_accass](ccode, cdept_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_item] ON [gl_accass](ccode, citem_class, citem_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_item_dept] ON [gl_accass](ccode, citem_class, citem_id, cdept_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_person] ON [gl_accass](ccode, cdept_id, cperson_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_post] ON [gl_accass](ccode, cdept_id, cperson_id, ccus_id, csup_id, citem_class, citem_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_sup] ON [gl_accass](ccode, csup_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_sup_dept] ON [gl_accass](ccode, csup_id, cdept_id, iperiod) ON [PRIMARY]
CREATE  INDEX [ccode_sup_item] ON [gl_accass](ccode, csup_id, citem_class, citem_id, iperiod) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table gl_CodeRemarkItem alter column  ccode nvarchar(40) not NULL
if exists(select * from sysindexes 
where id=object_id(N'GL_bmulti') and name='ccode')
drop index GL_bmulti.ccode 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'GL_bmulti') and name='ccodes')
drop index GL_bmulti.ccodes 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_bmulti alter column  ccode nvarchar(40) not NULL
alter table GL_bmulti alter column  ccodes nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [ccode] ON [GL_bmulti](ccode) ON [PRIMARY]
CREATE  INDEX [ccodes] ON [GL_bmulti](ccodes) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'GL_bMultiGroup') and name='cname_ccodes')
drop index GL_bMultiGroup.cname_ccodes
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_bMultiGroup alter column  ccode nvarchar(40) not NULL
alter table GL_bMultiGroup alter column  ccodes nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [cname_ccodes] ON [GL_bMultiGroup](cname, ccodes) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'dsigns') and name='ccode')
drop index dsigns.ccode 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table dsigns alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [ccode] ON [dsigns](ccode) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'gl_bdigest') and name='ccode')
drop index gl_bdigest.ccode 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table gl_bdigest alter column  ccode nvarchar(40)  NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [ccode] ON [gl_bdigest](ccode) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin

if exists(select * from sysindexes 
where id=object_id(N'gl_bfreq') and name='ccode')
drop index gl_bfreq.ccode 
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table gl_bfreq alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [ccode] ON [gl_bfreq](ccode) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'RP_cheque') and name='aaaaaRP_cheque_PK')
alter table RP_cheque drop constraint aaaaaRP_cheque_PK
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table RP_cheque alter column  ccode nvarchar(40) not NULL
alter table RP_cheque alter column  ccode_equal nvarchar(40)  NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table RP_cheque add constraint aaaaaRP_cheque_PK primary key(ccode, ccheque_id)
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists (select * from dbo.sysindexes where name = N'ccode' and id = object_id(N'[dbo].[GL_merror]'))
drop index [GL_merror].[ccode]
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table gl_merror alter column  ccode nvarchar(40) not NULL
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
CREATE  INDEX [ccode] ON gl_merror([ccode]) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin

if exists(select * from sysindexes 
where id=object_id(N'GL_mccontrol') and name='aaaaaGL_mccontrol_PK')
alter table GL_mccontrol drop constraint aaaaaGL_mccontrol_PK
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_mccontrol alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_mccontrol add constraint aaaaaGL_mccontrol_PK primary key(ccode)
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
if exists(select * from sysindexes 
where id=object_id(N'GL_mcodeused') and name='aaaaaGL_mcodeused_PK')
alter table GL_mcodeused drop constraint aaaaaGL_mcodeused_PK
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_mcodeused alter column  ccode nvarchar(40) not NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_mcodeused add constraint aaaaaGL_mcodeused_PK primary key(cuser, ccode)
end
go 
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table GL_VouchDraft alter column  ccode nvarchar(40) not NULL
alter table GL_VouchDraft alter column  cdigest nvarchar(120)  NULL
alter table GL_VouchDraft alter column  citem_id nvarchar(60)  NULL
alter table  GL_XmZcDetail alter column  ccode nvarchar(40) not NULL
alter table GL_YeMxSel alter column  ccode nvarchar(40) not NULL
end
GO
------------------总账end---------------------------------
-------------薪资和福利begin------------------------------
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table WA_GZFT alter column  cD_codeName nvarchar(40)
alter table WA_GZFT alter column  cC_codeName nvarchar(40)
alter table WA_GzsumComCfg alter column  Cdebit nvarchar(40)
alter table WA_GzsumComCfg alter column  cCreditSide nvarchar(40)
end
--alter table hr_wm_typesets alter column  vdebitsub nvarchar(40)
--alter table hr_wm_typesets alter column  vlendsub nvarchar(40)
--alter table hr_wm_apport alter column  vdebitsub nvarchar(40)
--alter table hr_wm_apport alter column  vlendsub nvarchar(40)
------------薪资和福利end----------------------------
--------------存货begin--------------------
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @ForeignKeyName nvarchar(200)
	declare @sSQL nvarchar(4000)

	declare ia_foreignKey_cursor cursor for
	select object_name(constid) from sysforeignkeys where fkeyid = object_id('IA_HeadSet')

	open ia_foreignKey_cursor

	FETCH NEXT FROM ia_foreignKey_cursor 
	INTO @ForeignKeyName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		set @sSQL = 'alter table ia_headset drop CONSTRAINT ' + @ForeignKeyName
		exec (@sSQL)
		FETCH NEXT FROM ia_foreignKey_cursor 
			INTO @ForeignKeyName
	END

	CLOSE ia_foreignKey_cursor
	DEALLOCATE ia_foreignKey_cursor
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @ForeignKeyName nvarchar(200)
	declare @sSQL nvarchar(4000)

	declare ia_foreignKey_cursor cursor for
	select object_name(constid) from sysforeignkeys where fkeyid = object_id('Ia_subsidiary')

	open ia_foreignKey_cursor

	FETCH NEXT FROM ia_foreignKey_cursor 
	INTO @ForeignKeyName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		set @sSQL = 'alter table Ia_subsidiary drop CONSTRAINT ' + @ForeignKeyName
		exec (@sSQL)
		FETCH NEXT FROM ia_foreignKey_cursor 
			INTO @ForeignKeyName
	END

	CLOSE ia_foreignKey_cursor
	DEALLOCATE ia_foreignKey_cursor
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @ForeignKeyName nvarchar(200)
	declare @sSQL nvarchar(4000)

	declare ia_foreignKey_cursor cursor for
	select object_name(constid) from sysforeignkeys where fkeyid = object_id('IA_MoneyPlan')

	open ia_foreignKey_cursor

	FETCH NEXT FROM ia_foreignKey_cursor 
	INTO @ForeignKeyName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		set @sSQL = 'alter table IA_MoneyPlan drop CONSTRAINT ' + @ForeignKeyName
		exec (@sSQL)
		FETCH NEXT FROM ia_foreignKey_cursor 
			INTO @ForeignKeyName
	END

	CLOSE ia_foreignKey_cursor
	DEALLOCATE ia_foreignKey_cursor
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @ForeignKeyName nvarchar(200)
	declare @sSQL nvarchar(4000)

	declare ia_foreignKey_cursor cursor for
	select object_name(constid) from sysforeignkeys where fkeyid = object_id('IA_Summary')

	open ia_foreignKey_cursor

	FETCH NEXT FROM ia_foreignKey_cursor 
	INTO @ForeignKeyName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		set @sSQL = 'alter table IA_Summary drop CONSTRAINT ' + @ForeignKeyName
		exec (@sSQL)
		FETCH NEXT FROM ia_foreignKey_cursor 
			INTO @ForeignKeyName
	END

	CLOSE ia_foreignKey_cursor
	DEALLOCATE ia_foreignKey_cursor
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @ForeignKeyName nvarchar(200)
	declare @sSQL nvarchar(4000)

	declare ia_foreignKey_cursor cursor for
	select object_name(constid) from sysforeignkeys where fkeyid = object_id('IA_ValuationAss')

	open ia_foreignKey_cursor

	FETCH NEXT FROM ia_foreignKey_cursor 
	INTO @ForeignKeyName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		set @sSQL = 'alter table IA_ValuationAss drop CONSTRAINT ' + @ForeignKeyName
		exec (@sSQL)
		FETCH NEXT FROM ia_foreignKey_cursor 
			INTO @ForeignKeyName
	END

	CLOSE ia_foreignKey_cursor
	DEALLOCATE ia_foreignKey_cursor
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	declare @ForeignKeyName nvarchar(200)
	declare @sSQL nvarchar(4000)

	declare ia_foreignKey_cursor cursor for
	select object_name(constid) from sysforeignkeys where fkeyid = object_id('JustInVouchs')

	open ia_foreignKey_cursor

	FETCH NEXT FROM ia_foreignKey_cursor 
	INTO @ForeignKeyName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		set @sSQL = 'alter table JustInVouchs drop CONSTRAINT ' + @ForeignKeyName
		exec (@sSQL)
		FETCH NEXT FROM ia_foreignKey_cursor 
			INTO @ForeignKeyName
	END

	CLOSE ia_foreignKey_cursor
	DEALLOCATE ia_foreignKey_cursor
end
GO
---- --存货编码扩到60
--ALTER TABLE Ia_subsidiary ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE Ia_Ensubsidiary ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_DecReadyHead ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_DecReadys ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_HeadSet ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_MaxMinCostDif ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_MoneyPlan ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_OppHead  ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_SubsidiaryPreAss ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_Summary ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_ValuationAss ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE JustInVouchs ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE CostJustVouchs ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_AccountMutex ALTER COLUMN cInvCode NVARCHAR(60)
--GO
--ALTER TABLE IA_DrawbakTaxSet ALTER COLUMN cInvCode NVARCHAR(60)
--GO
-- --科目编码扩到40
if exists (select 1 from @t6db..code where len(ccode)>15)
begin

	ALTER TABLE IA_Subsidiary ALTER COLUMN cInvHead NVARCHAR(40)
	
	ALTER TABLE IA_Subsidiary ALTER COLUMN cDifHead NVARCHAR(40)
	
	ALTER TABLE IA_Subsidiary ALTER COLUMN cOppHead NVARCHAR(40)
	
	ALTER TABLE Ia_Ensubsidiary ALTER COLUMN cOutHead NVARCHAR(40)
	
	ALTER TABLE Ia_Ensubsidiary ALTER COLUMN cOPPHead NVARCHAR(40)
	
	ALTER TABLE IA_DecReadyHead ALTER COLUMN cDecHead NVARCHAR(40)
	
	ALTER TABLE IA_DecReadyHead ALTER COLUMN cFyHead NVARCHAR(40)
	
	ALTER TABLE IA_DecReadys ALTER COLUMN cDecHead NVARCHAR(40)
	
	ALTER TABLE IA_HeadSet ALTER COLUMN cInvHead NVARCHAR(40)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[IA_HeadSet]') AND name = N'codeIA_HeadSet')
	DROP INDEX IA_HeadSet.codeIA_HeadSet
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	ALTER TABLE IA_HeadSet ALTER COLUMN cDifHead NVARCHAR(40)

	ALTER TABLE IA_HeadSet ALTER COLUMN cOutHead NVARCHAR(40)

	ALTER TABLE IA_HeadSet ALTER COLUMN cEnDispHead NVARCHAR(40)

	ALTER TABLE IA_HeadSet ALTER COLUMN czyHead NVARCHAR(40)

	ALTER TABLE IA_OppHead  ALTER COLUMN cOppHead NVARCHAR(40)

	ALTER TABLE IA_OppHead  ALTER COLUMN cEstHead NVARCHAR(40)

--ALTER TABLE IA_OppHead  ALTER COLUMN cMaterialHead NVARCHAR(40)
end
GO
--ALTER TABLE IA_OppHead  ALTER COLUMN cProcessHead NVARCHAR(40)

--ALTER TABLE IA_SubsidiaryPreAss ALTER COLUMN cInvHead NVARCHAR(40)

--ALTER TABLE IA_SubsidiaryPreAss ALTER COLUMN cDifHead NVARCHAR(40)

--ALTER TABLE IA_SubsidiaryPreAss ALTER COLUMN cOppHead NVARCHAR(40)


--alter table IA_DrawbakTaxSet alter column cDrawbackTaxCode nvarchar(40)

-----------------存货end-----------------------------------------



-- 科目扩至40
-- 处理: alter table code alter column ccode nvarchar(40) not null 以便验证:

--alter table ap_ctrlcode drop constraint FK__Ap_CtrlCo__cPreC__7C9A5A9E
--alter table ap_ctrlcode drop constraint FK__Ap_CtrlCo__cCtrl__7D8E7ED7
--alter table SaleBillVouchs drop constraint FK__SaleBillV__cTaxS__11957784
--alter table SaleBillVouchs drop constraint FK__SaleBillV__cInco__10A1534B

--alter table Rd_Style drop constraint FK__Rd_Style__cOppHe__0EB90AD9
--alter table PurBillVouchs drop constraint FK__PurBillVo__cDebi__0DC4E6A0
--alter table PurBillVouchs drop constraint FK__PurBillVo__cTaxH__0CD0C267

--alter table IA_HeadSet drop constraint FK__IA_HeadSe__cInvH__0BDC9E2E
--alter table IA_HeadSet drop constraint FK__IA_HeadSe__cDifH__0AE879F5
--alter table Ar_BadPara drop constraint FK__Ar_BadPar__cDyCo__0717E911
--alter table Ar_BadPara drop constraint FK__Ar_BadPar__cHzCo__0623C4D8
--alter table Ap_InvCode drop constraint FK__Ap_InvCod__cInpC__025333F4
--alter table Ap_InvCode drop constraint FK__Ap_InvCod__cInpC__015F0FBB
--alter table Ap_InvCode drop constraint FK__Ap_InvCod__cInpC__006AEB82
--alter table Ap_CloseBill drop constraint FK__Ap_CloseB__cCode__7BA63665

--alter table code drop constraint [aaaaacode_PK]
--alter table code alter column ccode nvarchar(40) not null 
--ALTER TABLE [code] ADD  CONSTRAINT [aaaaacode_PK] PRIMARY KEY NONCLUSTERED 
--(
--	[ccode] ASC
--) ON [PRIMARY]
--GO


-- FA固定资产: 生成备份表脚本之前执行:
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns   where   [name]='sDeprSubjectNum' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_Cards'))
		begin
			IF  EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[fa_Cards]') AND name = N'Codefa_Cards')
				DROP INDEX [fa_Cards].[Codefa_Cards]
			
			alter table fa_Cards alter column sDeprSubjectNum nvarchar(40) NULL
			
			IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[fa_Cards]') AND name = N'Codefa_Cards')
			CREATE NONCLUSTERED INDEX [Codefa_Cards] ON [fa_Cards] 
			(
				[sDeprSubjectNum] ASC
			) ON [PRIMARY]
		end
end
go
--if  exists(select * FROM syscolumns   where   [name]='sDefaultAssetSubjectNum' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_AssetTypes'))
--	begin
--		alter table fa_AssetTypes alter column sDefaultAssetSubjectNum nvarchar(40) NULL
--		alter table fa_AssetTypes alter column sDefaultDeprTotalSubjectNum nvarchar(40) NULL
--		alter table fa_AssetTypes alter column sDefaultDecPreSubjectNum nvarchar(40) NULL
--	end
--go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns   where   [name]='ccode' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_zwvouchers'))
		begin
			alter table fa_zwvouchers alter column ccode nvarchar(40) NULL
		end
end 
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns   where   [name]='sSubjectNum' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_Departments'))
		begin
			IF  EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[fa_Departments]') AND name = N'Codefa_departments')
			DROP INDEX [fa_Departments].[Codefa_departments]
			
			alter table fa_Departments alter column sSubjectNum nvarchar(40) NULL
			
			IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[fa_Departments]') AND name = N'Codefa_departments')
			CREATE NONCLUSTERED INDEX Codefa_departments ON [fa_Departments]
			(
				[sSubjectNum] ASC
			) ON [PRIMARY]
		end
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns   where   [name]='sSubjectNum' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_Origins'))
		begin
			if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__fa_Origin__sSubj__09F455BC') and xType=N'F')
			begin
				alter table fa_Origins drop constraint FK__fa_Origin__sSubj__09F455BC
			end
			IF  EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[fa_Origins]') AND name = N'Codefa_origins')
				DROP INDEX [fa_Origins].[Codefa_origins]
			
			--alter table fa_Origins alter column sSubjectNum varchar(40) NULL
			alter table fa_Origins alter column sSubjectNum nvarchar(40) NULL
			
			IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[fa_Origins]') AND name = N'Codefa_origins')
				CREATE NONCLUSTERED INDEX [Codefa_origins] ON [fa_Origins] 
				(
					[sSubjectNum] ASC
				) ON [PRIMARY]

			if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__fa_Origin__sSubj__09F455BC') AND type = 'F')
				alter table fa_Origins add constraint FK__fa_Origin__sSubj__09F455BC foreign key (sSubjectNum) references code (ccode)
		end
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns   where   [name]='sSubjectNum' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_DeprVouchers'))
	begin
		alter table fa_DeprVouchers alter column sSubjectNum nvarchar(40) NULL
	end
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns   where   [name]='sSubjectNum' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('fa_DeprVouchers_pre'))
	begin
		alter table fa_DeprVouchers_pre alter column sSubjectNum nvarchar(40) NULL
	end
end
go


-- NB网上银行:
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if  exists(select * FROM syscolumns  where   [name]='cBankCode' and  OBJECTPROPERTY(id,'IsUserTable')=1   and   id=OBject_id('NB_PayUnitInfo'))
	begin
		alter table NB_PayUnitInfo alter column cBankCode nvarchar(40) NULL
	end
end
go

-- PM项目管理:
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	alter table PM_ItemBill alter column cJFCode nvarchar(40)
	alter table PM_ItemBill alter column cDFCode nvarchar(40)

	alter table PM_ItemElement alter column cCode nvarchar(40)
	alter table PM_ItemElement alter column cCodeJZ nvarchar(40)

	alter table PM_PzSet alter column cJFCode nvarchar(40)
	alter table PM_PzSet alter column cDFCode nvarchar(40)

--GO
--alter table PM_PzSetSub alter column cJFCode nvarchar(60) not null
--alter table PM_PzSetSub alter column cDFCode nvarchar(60) not null
--GO

	alter table PM_PzSetBill alter column cJFCode nvarchar(40)
	alter table PM_PzSetBill alter column cDFCode nvarchar(40)
end

-- alter table PM_TranControl alter column cCode1 nvarchar(40) null
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	alter table PM_ItemTranDef alter column cSaleCode	 nvarchar(40) not null
	alter table PM_ItemTranDef alter column cInComeCode	 nvarchar(40) not null
	alter table PM_ItemTranDef alter column cProWareCode nvarchar(40) not null
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
-- AP收付:
IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[Ap_Detail]') AND name = N'cKmCode')
	DROP INDEX [Ap_Detail].[cKmCode]
alter table ap_detail alter column ccode nvarchar(40)		
IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[Ap_Detail]') AND name = N'cKmCode')
	CREATE NONCLUSTERED INDEX [cKmCode] ON [Ap_Detail] 
	(
		[cCode] ASC
	) ON [PRIMARY]
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_CloseB__cCode__7BA63665') and xType=N'F')
		alter table ap_closebill drop constraint FK__Ap_CloseB__cCode__7BA63665
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_closebill]') AND name = N'codeAp_CloseBill')
		DROP INDEX ap_closebill.codeAp_CloseBill
	alter table ap_closebill alter column ccode nvarchar(40)		
	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_closebill]') AND name = N'codeAp_CloseBill')
		CREATE NONCLUSTERED INDEX codeAp_CloseBill ON [ap_closebill] 
		(
			[cCode] ASC
		) ON [PRIMARY]
	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_CloseB__cCode__7BA63665') AND type = 'F')
		alter table ap_closebill add constraint FK__Ap_CloseB__cCode__7BA63665 foreign key (ccode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
alter table ap_closebills alter column ckm nvarchar(40)
end
GO

-- ap_vouch
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_Vouch__cCode__043B7C66') and xType=N'F')
		alter table ap_vouch drop constraint FK__Ap_Vouch__cCode__043B7C66
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_vouch]') AND name = N'codeAp_Vouch')
		DROP INDEX ap_vouch.codeAp_Vouch

	alter table ap_vouch alter column ccode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_vouch]') AND name = N'codeAp_Vouch')
		CREATE NONCLUSTERED INDEX codeAp_Vouch ON [ap_vouch] 
		(
			[cCode] ASC
		) ON [PRIMARY]
		
	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_Vouch__cCode__043B7C66') AND type = 'F')
		alter table ap_vouch add constraint FK__Ap_Vouch__cCode__043B7C66 foreign key (ccode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- ap_vouchs
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_Vouchs__cCode__052FA09F') and xType=N'F')
		alter table ap_vouchs drop constraint FK__Ap_Vouchs__cCode__052FA09F
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_vouchs]') AND name = N'codeAp_Vouchs')
		DROP INDEX ap_vouchs.codeAp_Vouchs

	alter table ap_vouchs alter column ccode nvarchar(40)
	-- alter table ap_vouchs alter column ccode varchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_vouchs]') AND name = N'codeAp_Vouchs')
		CREATE NONCLUSTERED INDEX codeAp_Vouchs ON [ap_vouchs] 
		(
			[cCode] ASC
		) ON [PRIMARY]
		
	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_Vouchs__cCode__052FA09F') AND type = 'F')
		alter table ap_vouchs add constraint FK__Ap_Vouchs__cCode__052FA09F foreign key (ccode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- ap_note
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_note]') AND name = N'ccode')
		DROP INDEX ap_note.ccode
	alter table ap_note alter column ccode nvarchar(40)
	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_note]') AND name = N'ccode')
		CREATE NONCLUSTERED INDEX ccode ON [ap_note] 
		(
			[cCode] ASC
		) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	alter table ap_note_sub alter column ccode nvarchar(40)
end
GO
-- alter table ap_applypayvouch alter column ccode nvarchar(40)
-- GO

-- ap_inputcode
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_InputC__cArCo__7E82A310') and xType=N'F')
		alter table ap_inputcode drop constraint FK__Ap_InputC__cArCo__7E82A310
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_inputcode]') AND name = N'codeAp_InputCode')
		DROP INDEX ap_inputcode.codeAp_InputCode

	alter table ap_inputcode alter column carcode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_inputcode]') AND name = N'codeAp_InputCode')
		CREATE NONCLUSTERED INDEX codeAp_InputCode ON [ap_inputcode] 
		(
			[carcode] ASC
		) ON [PRIMARY]
		
	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_InputC__cArCo__7E82A310') AND type = 'F')
		alter table ap_inputcode add constraint FK__Ap_InputC__cArCo__7E82A310 foreign key (carcode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
-- alter table ap_inputcode alter column capcode nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_InputC__cApCo__7F76C749') and xType=N'F')
		alter table ap_inputcode drop constraint FK__Ap_InputC__cApCo__7F76C749
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_inputcode]') AND name = N'codeAp_InputCode1')
		DROP INDEX ap_inputcode.codeAp_InputCode1

	alter table ap_inputcode alter column capcode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_inputcode]') AND name = N'codeAp_InputCode1')
		CREATE NONCLUSTERED INDEX codeAp_InputCode1 ON [ap_inputcode] 
		(
			[carcode] ASC
		) ON [PRIMARY]
		
	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_InputC__cApCo__7F76C749') AND type = 'F')
		alter table ap_inputcode add constraint FK__Ap_InputC__cApCo__7F76C749 foreign key (capcode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
-- alter table ap_ctrlcode alter column cCtrlCode nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_CtrlCo__cCtrl__7D8E7ED7') and xType=N'F')
		alter table ap_ctrlcode drop constraint FK__Ap_CtrlCo__cCtrl__7D8E7ED7
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'codeAp_CtrlCode1')
		DROP INDEX ap_ctrlcode.codeAp_CtrlCode1

	alter table ap_ctrlcode alter column cCtrlCode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'codeAp_CtrlCode1')
		CREATE NONCLUSTERED INDEX codeAp_CtrlCode1 ON [ap_ctrlcode] 
		(
			[cCtrlCode] ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_CtrlCo__cCtrl__7D8E7ED7') AND type = 'F')
		alter table ap_ctrlcode add constraint FK__Ap_CtrlCo__cCtrl__7D8E7ED7 foreign key (cCtrlCode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table ap_ctrlcode alter column cPreCode nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_CtrlCo__cPreC__7C9A5A9E') and xType=N'F')
		alter table ap_ctrlcode drop constraint FK__Ap_CtrlCo__cPreC__7C9A5A9E
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'codeAp_CtrlCode')
		DROP INDEX ap_ctrlcode.codeAp_CtrlCode 

	alter table ap_ctrlcode alter column cPreCode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'codeAp_CtrlCode')
		CREATE NONCLUSTERED INDEX codeAp_CtrlCode ON [ap_ctrlcode] 
		(
			cPreCode ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_CtrlCo__cPreC__7C9A5A9E') AND type = 'F')
		alter table ap_ctrlcode add constraint FK__Ap_CtrlCo__cPreC__7C9A5A9E foreign key (cPreCode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table ap_ctrlcode alter column cSkCode nvarchar(40)
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'cSkCode')
		DROP INDEX ap_ctrlcode.cSkCode 

	alter table ap_ctrlcode alter column cSkCode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'cSkCode')
		CREATE NONCLUSTERED INDEX cSkCode ON [ap_ctrlcode] 
		(
			cSkCode ASC
		) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table ap_ctrlcode alter column cPreSkCode nvarchar(40)
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'cPreSkCode')
		DROP INDEX ap_ctrlcode.cPreSkCode

	alter table ap_ctrlcode alter column cPreSkCode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_ctrlcode]') AND name = N'cPreSkCode')
		CREATE NONCLUSTERED INDEX cPreSkCode ON [ap_ctrlcode] 
		(
			cPreSkCode ASC
		) ON [PRIMARY]
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	--  alter table ap_invcode alter column cInpCode1 nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_InvCod__cInpC__025333F4') and xType=N'F')
		alter table ap_invcode drop constraint FK__Ap_InvCod__cInpC__025333F4
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_invcode]') AND name = N'codeAp_InvCode2')
		DROP INDEX ap_invcode.codeAp_InvCode2 

	alter table ap_invcode alter column cInpCode1 nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_invcode]') AND name = N'codeAp_InvCode2')
		CREATE NONCLUSTERED INDEX codeAp_InvCode2 ON [ap_invcode] 
		(
			cInpCode1 ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_InvCod__cInpC__025333F4') AND type = 'F')
		alter table ap_invcode add constraint FK__Ap_InvCod__cInpC__025333F4 foreign key (cInpCode1) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	--  alter table ap_invcode alter column cInpCode2 nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_InvCod__cInpC__006AEB82') and xType=N'F')
		alter table ap_invcode drop constraint FK__Ap_InvCod__cInpC__006AEB82
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_invcode]') AND name = N'codeAp_InvCode')
		DROP INDEX ap_invcode.codeAp_InvCode

	alter table ap_invcode alter column cInpCode2 nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_invcode]') AND name = N'codeAp_InvCode')
		CREATE NONCLUSTERED INDEX codeAp_InvCode ON [ap_invcode] 
		(
			cInpCode2 ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_InvCod__cInpC__006AEB82') AND type = 'F')
		alter table ap_invcode add constraint FK__Ap_InvCod__cInpC__006AEB82 foreign key (cInpCode2) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table ap_invcode alter column cInpCode3 nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_InvCod__cInpC__015F0FBB') and xType=N'F')
		alter table ap_invcode drop constraint FK__Ap_InvCod__cInpC__015F0FBB
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_invcode]') AND name = N'codeAp_InvCode1')
		DROP INDEX ap_invcode.codeAp_InvCode1

	alter table ap_invcode alter column cInpCode3 nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_invcode]') AND name = N'codeAp_InvCode1')
		CREATE NONCLUSTERED INDEX codeAp_InvCode1 ON [ap_invcode] 
		(
			cInpCode3 ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_InvCod__cInpC__015F0FBB') AND type = 'F')
		alter table ap_invcode add constraint FK__Ap_InvCod__cInpC__015F0FBB foreign key (cInpCode3) references code (ccode)
end
GO

if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table ap_sstylecode alter column ccode nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ap_SStyle__cCode__0347582D') and xType=N'F')
		alter table ap_sstylecode drop constraint FK__Ap_SStyle__cCode__0347582D
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_sstylecode]') AND name = N'codeAp_SStyleCode')
		DROP INDEX ap_sstylecode.codeAp_SStyleCode 

	alter table ap_sstylecode alter column ccode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[ap_sstylecode]') AND name = N'codeAp_SStyleCode')
		CREATE NONCLUSTERED INDEX codeAp_SStyleCode ON [ap_sstylecode] 
		(
			ccode ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ap_SStyle__cCode__0347582D') AND type = 'F')
		alter table ap_sstylecode add constraint FK__Ap_SStyle__cCode__0347582D foreign key (ccode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- ar_badpara
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ar_BadPar__cHzCo__0623C4D8') and xType=N'F')
		alter table ar_badpara drop constraint FK__Ar_BadPar__cHzCo__0623C4D8
		
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__Ar_BadPar__cDyCo__0717E911') and xType=N'F')
		alter table ar_badpara drop constraint FK__Ar_BadPar__cDyCo__0717E911
		
	alter table ar_badpara alter column cHzCode nvarchar(40)
	alter table ar_badpara alter column cDyCode nvarchar(40)

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ar_BadPar__cHzCo__0623C4D8') AND type = 'F')
		alter table ar_badpara add constraint FK__Ar_BadPar__cHzCo__0623C4D8 foreign key (cHzCode) references code (ccode)

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__Ar_BadPar__cDyCo__0717E911') AND type = 'F')
		alter table ar_badpara add constraint FK__Ar_BadPar__cDyCo__0717E911 foreign key (cDyCode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table SaleBillVouch alter column cHeadCode nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__SaleBillV__cHead__0FAD2F12') and xType=N'F')
		alter table SaleBillVouch drop constraint FK__SaleBillV__cHead__0FAD2F12
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[SaleBillVouch]') AND name = N'codeSaleBillVouch1')
		DROP INDEX SaleBillVouch.codeSaleBillVouch1 

	alter table SaleBillVouch alter column cHeadCode nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[SaleBillVouch]') AND name = N'codeSaleBillVouch1')
		CREATE NONCLUSTERED INDEX codeSaleBillVouch1 ON [SaleBillVouch] 
		(
			cHeadCode ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__SaleBillV__cHead__0FAD2F12') AND type = 'F')
		alter table SaleBillVouch add constraint FK__SaleBillV__cHead__0FAD2F12 foreign key (cHeadCode) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
	begin	
	-- alter table SaleBillVouchs alter column cIncomeSub nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__SaleBillV__cInco__10A1534B') and xType=N'F')
		alter table SaleBillVouchs drop constraint FK__SaleBillV__cInco__10A1534B
		
	alter table SaleBillVouchs alter column cIncomeSub nvarchar(40)

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__SaleBillV__cInco__10A1534B') AND type = 'F')
		alter table SaleBillVouchs add constraint FK__SaleBillV__cInco__10A1534B foreign key (cIncomeSub) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
alter table PurBillVouch alter column cHeadCode nvarchar(40)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin	
	-- alter table PurBillVouchs alter column cDebitHead nvarchar(40)
	if exists(select top 1 [name] FROM sysobjects where id=object_id('FK__PurBillVo__cDebi__0DC4E6A0') and xType=N'F')
		alter table PurBillVouchs drop constraint FK__PurBillVo__cDebi__0DC4E6A0
		
	IF EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[PurBillVouchs]') AND name = N'codePurBillVouchs1')
		DROP INDEX PurBillVouchs.codePurBillVouchs1

	alter table PurBillVouchs alter column cDebitHead nvarchar(40)

	IF not EXISTS (SELECT * FROM sysindexes WHERE id = OBJECT_ID(N'[PurBillVouchs]') AND name = N'codePurBillVouchs1')
		CREATE NONCLUSTERED INDEX codePurBillVouchs1 ON [PurBillVouchs] 
		(
			cDebitHead ASC
		) ON [PRIMARY]

	if not exists (select 1 FROM sysobjects WHERE id = object_id('FK__PurBillVo__cDebi__0DC4E6A0') AND type = 'F')
		alter table PurBillVouchs add constraint FK__PurBillVo__cDebi__0DC4E6A0 foreign key (cDebitHead) references code (ccode)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
	if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Rd_Style_ContraRef]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
	CREATE TABLE [Rd_Style_ContraRef] (
		[cRdCode] [varchar] (5) COLLATE Chinese_PRC_CI_AS NOT NULL ,
		[cRdName] [varchar] (12) COLLATE Chinese_PRC_CI_AS NOT NULL ,
		[iRdGrade] [tinyint] NOT NULL ,
		[bRdFlag] [int] NOT NULL ,
		[bRdEnd] [bit] NOT NULL ,
		[cOppHead] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
		[pubufts] [timestamp] NULL ,
		CONSTRAINT [aaaaaRd_Style_ContraRef_PK] PRIMARY KEY  NONCLUSTERED 
		(
			[cRdCode]
		)  ON [PRIMARY] ,
		 FOREIGN KEY 
		(
			[cOppHead]
		) REFERENCES [code] (
			[ccode]
		)
	) ON [PRIMARY]
end
go

---------------专家财务begin-----------

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'aaaaaCW_DeptPlan_PK' 
	  AND 	 type = 'K')
ALTER TABLE CW_DEPTPLAN DROP CONSTRAINT [aaaaaCW_DeptPlan_PK]
end
GO

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'aaaaaCW_ProjPlan_PK' 
	  AND 	 type = 'K')
ALTER TABLE cw_projplan DROP CONSTRAINT aaaaaCW_ProjPlan_PK
end
GO


if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'aaaaaCW_CodePlan_PK' 
	  AND 	 type = 'K')
ALTER TABLE CW_CodePlan DROP CONSTRAINT aaaaaCW_CodePlan_PK
end
GO

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'CW_WideDeptCode_PK' 
	  AND 	 type = 'K')
ALTER TABLE CW_WideDeptCode DROP CONSTRAINT CW_WideDeptCode_PK
end
GO


if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'PK_CW_WideProjCode' 
	  AND 	 type = 'K')
ALTER TABLE CW_WideProjCode DROP CONSTRAINT PK_CW_WideProjCode
end
GO

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table [CW_CodePlan]
alter column [AccountCode] [nvarchar](40) NOT NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table [CW_CodePlus]
alter column [AccountCode] [nvarchar](40) NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table [CW_DeptPlan]
alter column [AccountCode] [nvarchar](40) NOT NULL
end
go

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter table [CW_DeptPlus]
alter column [AccountCode] [nvarchar](40) NULL
end
go


if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter TABLE [CW_ProjPlan]
alter column [AccountCode] [nvarchar](40) NOT NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter TABLE [CW_ProjPlus]
alter column [AccountCode] [nvarchar](40) NULL
end
go
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter TABLE [CW_WideDeptCode]
alter column [AccountCode] [nvarchar](40) NOT NULL
end
go

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
alter TABLE [CW_WideProjCode]
alter column [AccountCode] [nvarchar](40) NOT NULL
end
go

if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF NOT EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'aaaaaCW_DeptPlan_PK' 
	  AND 	 type = 'K')
alter table CW_DEPTPLAN add CONSTRAINT [aaaaaCW_DeptPlan_PK] PRIMARY KEY ([DeptID] ASC,[AccountCode] ASC)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF NOT EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'[aaaaaCW_ProjPlan_PK]' 
	  AND 	 type = 'K')
alter table cw_projplan add CONSTRAINT [aaaaaCW_ProjPlan_PK] 
PRIMARY KEY ([ProjectClassID] ASC,[ProjectID] ASC,[AccountCode] ASC)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF NOT EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'aaaaaCW_CodePlan_PK' 
	  AND 	 type = 'K')
alter table CW_CodePlan add CONSTRAINT [aaaaaCW_CodePlan_PK] PRIMARY KEY ([AccountCode] ASC)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF NOT EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'CW_WideDeptCode_PK' 
	  AND 	 type = 'K')
alter table CW_WideDeptCode add CONSTRAINT [CW_WideDeptCode_PK] 
PRIMARY KEY (
       [DeptID] ASC,
	[AccountCode] ASC,
	[Direction] ASC
)
end
GO
if exists (select 1 from @t6db..code where len(ccode)>15)
begin
IF NOT EXISTS(SELECT name 
	  FROM 	 sysobjects 
	  WHERE  name = N'PK_CW_WideProjCode' 
	  AND 	 type = 'K')
alter table CW_WideProjCode add CONSTRAINT PK_CW_WideProjCode 
PRIMARY KEY (
   	[ProjectClassID] ASC,
	[ProjectID] ASC,
	[AccountCode] ASC,
	[Direction] ASC
)
end
GO
-------------专家财务end----------------------
----------科目扩位end--------------
--总账处理行业性质
if exists (select 1 from @t6db..sysobjects where name ='ua_account_ex' and xtype ='U')
begin
	alter table @t6db..ua_account_ex  alter column ctradekind varchar(40)
	update @t6db..ua_account_ex set ctradekind=N'2013年小企业会计准则' 
	where ctradekind=N'小企业会计准则（2013年）' 
	update @t6db..ua_account_ex set ctradekind=N'2007 年新会计准则科目' 
	where ctradekind=N'2007年新会计制度科目' or ctradekind=N'2007年新会计准则科目' 
	update @t6db..ua_account_ex set ctradekind=N'2010年新工会会计制度' 
	where ctradekind=N'工会会计制度' 
	update @t6db..ua_account_ex set ctradekind=N'2011年医院会计制度' 
	where ctradekind=N'医院会计制度(2011年)' 
	update @t6db..ua_account_ex set ctradekind=N'2011年基层医疗卫生机构会计制度' 
	where ctradekind=N'基层医疗卫生机构' 
end
go

-- 处理个别T6帐套Ap_InputCode 表主键丢失问题：
delete FROM @t6db..Ap_InputCode where isnull(cArCode,'') = '' and isnull(cArCodeName,'') = '' 
                            and isnull(cApCode,'') = '' and isnull(cApCodeName,'') = '' 
GO

-- 处理动态结构数据表 
drop table @u8db..[fa_ItemsManual] 
select * into @u8db.. [fa_ItemsManual] from @t6db..[fa_ItemsManual] where 1=0
alter table @u8db..[fa_ItemsManual] add CONSTRAINT [fa_ItemsManual_PK] PRIMARY KEY NONCLUSTERED( sCardNum)
GO

-- 处理备份表
declare @i as int ,@j as int ,@iMonth int, @strSql as nvarchar(4000)
set @i =(select MAX(iperiod) from @t6db..gl_mend where bflag_fa=1)
-- set @j =(select MIN(iperiod) from @t6db..gl_mend where bflag_fa=1 AND IPERIOD > 0)

if exists(select cValue from @t6db..AccInformation where cSysID = 'fa' and cname = 'dstartdate' and ISNULL(cValue,'') ='')
	set @j = 13
else
	set @j =(select a.iId from @t6db..AccInformation 
			left join #UA_Period a on (cAcc_Id = '@cAcc_Id' and iYear = '@iYear' and cValue between dbegin and dend)
			where cSysID = 'fa' and cname = 'dstartdate')

if @j <= @i
begin
set @iMonth = @j
while @iMonth<=@i
begin
	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_AccInformation_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_AccInformation_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='AccInformation')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_AccInformation_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select cSysID, cID, cName, cCaption, cType, cValue, cDefault, bVisible, bEnable into @u8db..UFFA_BAK_AccInformation_'+ltrim(str(@iMonth))+' from @u8db..AccInformation where 1=0 '
			exec(@strsql)
		end
	end

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_EQ_SendUser_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_EQ_SendUser_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='EQ_SendUser')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_EQ_SendUser_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_EQ_SendUser_'+ltrim(str(@iMonth))+' from @u8db..EQ_SendUser where 1=0 '
			exec(@strsql)
		end
	end
	
	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_AssetAttached_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_AssetAttached_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_AssetAttached')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_AssetAttached_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select lID, sCardNum, sNum, sName, sStyle, sUnit, lQuantity, dblValue, dDate, memRemark, sMark, sLockedBy into @u8db..UFFA_BAK_fa_AssetAttached_'+ltrim(str(@iMonth))+' from @u8db..fa_AssetAttached where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_AssetGroup_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_AssetGroup_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_AssetGroup')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_AssetGroup_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_AssetGroup_'+ltrim(str(@iMonth))+' from @u8db..fa_AssetGroup where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_AssetTypes_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_AssetTypes_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_AssetTypes')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_AssetTypes_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_AssetTypes_'+ltrim(str(@iMonth))+' from @u8db..fa_AssetTypes where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_CardOfInv_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_CardOfInv_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_CardOfInv')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_CardOfInv_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_CardOfInv_'+ltrim(str(@iMonth))+' from @u8db..fa_CardOfInv where 1=0 '
			exec(@strsql)
		end
	end

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_CardOfPUColSet_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_CardOfPUColSet_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_CardOfPUColSet')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_CardOfPUColSet_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_CardOfPUColSet_'+ltrim(str(@iMonth))+' from @u8db..fa_CardOfPUColSet where 1=0 '
			exec(@strsql)
		end
	end
	
	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Cards_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Cards_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Cards')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Cards_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select 
	sCardID,sCardNum,sAssetNum,sAssetName,lOptID,iOptType,iNewType,sZWVoucherType,sZWVoucherNum,sDeptNum,sTypeNum,sOrgID,sOrgAddID,sOrgDisposeID,sDisposeReason,sStatusID,sDeprMethodID,sCurrency,sDeprSubjectNum,sDeprSubjectName,sProjectNum,sProjectName,sStyle,dblBuildArea,
	lBuildNum,lMachinNum,dblMachinW,sSite,lLife,dStartdate,dInputDate,dTransDate,dDisposeDate,iInputPeriod,iTransPeriod,iDisposePeriod,dblFCValue,dblExchangeRate,sOperator,sDisposer,dblNetValueAct,lDeprMonthsAct,dblCanWorkAct,dblLegacy,sVoucherNum,sEvaluateNum,dblValue,dblDecDeprT,dblDecDepr,dblDecDeprRate,lDecDeprMonths,
	dblDecWorkT,dblDecPerDepr,dblBV,dblBVRate,dblCanWork,dblPerDepr,lUsedMonths,sWorkUnit,dblTransInDeprTCard,dblTransOutDeprTCard,iVouchsNextMonthAct,sTransTypeNum,sTransDepteNum,sLockedBy,dblDecPreValueT,dblRetDecPreValueT,bMultiDept,sDeptNames
			 into @u8db..UFFA_BAK_fa_Cards_'+ltrim(str(@iMonth))+' from @u8db..fa_Cards where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Cards_Detail_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Cards_Detail_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Cards_Detail')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Cards_Detail_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sCardID, sCardNum, sDeptNum, lOptID, dDisposeDate, iDisposePeriod, dblValue, dblDecDeprT, dblBV, dblCanWork, dblTransInDeprTCard, dblTransOutDeprTCard, dblDecPreValueT, dblRetDecPreValueT, dblBuildArea, lBuildNum, lMachinNum, dblMachinW into @u8db..UFFA_BAK_fa_Cards_Detail_'+ltrim(str(@iMonth))+' from @u8db..fa_Cards_Detail where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Cards_log_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Cards_log_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Cards_log')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Cards_log_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Cards_log_'+ltrim(str(@iMonth))+' from @u8db..fa_Cards_log where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_CardsSheets_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_CardsSheets_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_CardsSheets')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_CardsSheets_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sID, sCardNum, lSheetNum, sNum, sVoucherNum, sEvaNum, dStartDate, dEndDate, memReason, memRemark, dTransDate, sDeptName, lDays, dblAfterValue, dblTransValue, sMark, sLockedBy into @u8db..UFFA_BAK_fa_CardsSheets_'+ltrim(str(@iMonth))+' from @u8db..fa_CardsSheets where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_CheckData_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_CheckData_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_CheckData')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_CheckData_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_CheckData_'+ltrim(str(@iMonth))+' from @u8db..fa_CheckData where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_checklists_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_checklists_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_checklists')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_checklists_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_checklists_'+ltrim(str(@iMonth))+' from @u8db..fa_checklists where 1=0 '
			exec(@strsql)
		end
	end

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_CheckMain_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_CheckMain_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_CheckMain')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_CheckMain_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_CheckMain_'+ltrim(str(@iMonth))+' from @u8db..fa_CheckMain where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_CheckSetup_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_CheckSetup_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_CheckSetup')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_CheckSetup_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_CheckSetup_'+ltrim(str(@iMonth))+' from @u8db..fa_CheckSetup where 1=0 '
			exec(@strsql)
		end
	end
	
	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Control_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Control_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Control')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Control_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Control_'+ltrim(str(@iMonth))+' from @u8db..fa_Control where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Depreciations_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Depreciations_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Depreciations')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Depreciations_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Depreciations_'+ltrim(str(@iMonth))+' from @u8db..fa_Depreciations where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeprList_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeprList_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeprList')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeprList_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_DeprList_'+ltrim(str(@iMonth))+' from @u8db..fa_DeprList where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeprTransactions_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeprTransactions_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeprTransactions')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeprTransactions_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_DeprTransactions_'+ltrim(str(@iMonth))+' from @u8db..fa_DeprTransactions where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeprTransactions_Detail_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeprTransactions_Detail_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeprTransactions_Detail')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeprTransactions_Detail_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sCardNum, sDeptNum, dblDepr1, dblDeprT1, dblDepr2, dblDeprT2, dblDepr3, dblDeprT3, dblDepr4, dblDeprT4, dblDepr5, dblDeprT5, dblDepr6, dblDeprT6, dblDepr7, dblDeprT7, dblDepr8, dblDeprT8, dblDepr9, dblDeprT9, dblDepr10, dblDeprT10, dblDepr11, dblDeprT11, dblDepr12, dblDeprT12, dblMonthValue1, dblMonthValue2, dblMonthValue3, dblMonthValue4, dblMonthValue5, dblMonthValue6, dblMonthValue7, dblMonthValue8, dblMonthValue9, dblMonthValue10, dblMonthValue11, dblMonthValue12, dblMonthValue13, dblDeprThisYear  into @u8db..UFFA_BAK_fa_DeprTransactions_Detail_'+ltrim(str(@iMonth))+' from @u8db..fa_DeprTransactions_Detail where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeprVoucherMain_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeprVoucherMain_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeprVoucherMain')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeprVoucherMain_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_DeprVoucherMain_'+ltrim(str(@iMonth))+' from @u8db..fa_DeprVoucherMain where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeprVouchers_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeprVouchers_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeprVouchers')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeprVouchers_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select lOptID, iPeriod, sDeptNum, sDeptName, sTypeNum, sTypeName, sProjectNum, sProjectName, sSubjectNum, sSubjectName, dblDebit, dblCredit, sLockedBy into @u8db..UFFA_BAK_fa_DeprVouchers_'+ltrim(str(@iMonth))+' from @u8db..fa_DeprVouchers where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeprVouchers_pre_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeprVouchers_pre_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeprVouchers_pre')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeprVouchers_pre_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select lOptID, iPeriod, sDeptNum, sDeptName, sTypeNum, sTypeName, sProjectNum, sProjectName, sSubjectNum, sSubjectName, dblDebit, dblCredit, sLockedBy into @u8db..UFFA_BAK_fa_DeprVouchers_pre_'+ltrim(str(@iMonth))+' from @u8db..fa_DeprVouchers_pre where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_DeptScale_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_DeptScale_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_DeptScale')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_DeptScale_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sID,sCardNum, lOptID,sDeptNum,dblScale,sDeprSubjectNum,sDeprSubjectName,sProjectNum,sProjectName,cItemclsId into @u8db..UFFA_BAK_fa_DeptScale_'+ltrim(str(@iMonth))+' from @u8db..fa_DeptScale where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Dictionary_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Dictionary_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Dictionary')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Dictionary_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Dictionary_'+ltrim(str(@iMonth))+' from @u8db..fa_Dictionary where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_EvaluateMain_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_EvaluateMain_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_EvaluateMain')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_EvaluateMain_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_EvaluateMain_'+ltrim(str(@iMonth))+' from @u8db..fa_EvaluateMain where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_EvaluateVouchers_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_EvaluateVouchers_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_EvaluateVouchers')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_EvaluateVouchers_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_EvaluateVouchers_'+ltrim(str(@iMonth))+' from @u8db..fa_EvaluateVouchers where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_EvaluateVouchers_Detail_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_EvaluateVouchers_Detail_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_EvaluateVouchers_Detail')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_EvaluateVouchers_Detail_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sNum, lOptID, sCardNum, sDeptNum, dDate, iPeriod, dblValueBefore, dblValue, dblDeprTotalBefore, dblDeprTotal into @u8db..UFFA_BAK_fa_EvaluateVouchers_Detail_'+ltrim(str(@iMonth))+' from @u8db..fa_EvaluateVouchers_Detail where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Items_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Items_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Items')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Items_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Items_'+ltrim(str(@iMonth))+' from @u8db..fa_Items where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_ItemsManual_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_ItemsManual_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_ItemsManual')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_ItemsManual_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_ItemsManual_'+ltrim(str(@iMonth))+' from @u8db..fa_ItemsManual where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_ItemsOfModel_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_ItemsOfModel_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_ItemsOfModel')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_ItemsOfModel_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sModelID, iSheetID, sItemID, lItemRow, lItemStartCol, lItemEndCol, lInputCol, bSameCaption, sLockedBy into @u8db..UFFA_BAK_fa_ItemsOfModel_'+ltrim(str(@iMonth))+' from @u8db..fa_ItemsOfModel where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_ItemsOfQuery_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_ItemsOfQuery_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_ItemsOfQuery')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_ItemsOfQuery_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select iQuerysType, sName into @u8db..UFFA_BAK_fa_ItemsOfQuery_'+ltrim(str(@iMonth))+' from @u8db..fa_ItemsOfQuery where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_JKItemSet_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_JKItemSet_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_JKItemSet')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_JKItemSet_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_JKItemSet_'+ltrim(str(@iMonth))+' from @u8db..fa_JKItemSet where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_JKSet_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_JKSet_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_JKSet')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_JKSet_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_JKSet_'+ltrim(str(@iMonth))+' from @u8db..fa_JKSet where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Models_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Models_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Models')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Models_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Models_'+ltrim(str(@iMonth))+' from @u8db..fa_Models where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Objects_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Objects_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Objects')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Objects_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Objects_'+ltrim(str(@iMonth))+' from @u8db..fa_Objects where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Origins_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Origins_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Origins')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Origins_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Origins_'+ltrim(str(@iMonth))+' from @u8db..fa_Origins where 1=0 '
			exec(@strsql)
		end
	end

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_QueryFilters_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_QueryFilters_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_QueryFilters')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_QueryFilters_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sQueryID, sItemID, sItemType, sAndOr, sLeftBracket, sComparison, sWordValue, sRightBracket, sLockedBy into @u8db..UFFA_BAK_fa_QueryFilters_'+ltrim(str(@iMonth))+' from @u8db..fa_QueryFilters where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Querys_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Querys_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Querys')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Querys_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Querys_'+ltrim(str(@iMonth))+' from @u8db..fa_Querys where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_splitCards_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_splitCards_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_splitCards')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_splitCards_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_splitCards_'+ltrim(str(@iMonth))+' from @u8db..fa_splitCards where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Status_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Status_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Status')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Status_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Status_'+ltrim(str(@iMonth))+' from @u8db..fa_Status where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Total_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Total_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Total')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Total_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Total_'+ltrim(str(@iMonth))+' from @u8db..fa_Total where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Vouchers_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Vouchers_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Vouchers')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Vouchers_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Vouchers_'+ltrim(str(@iMonth))+' from @u8db..fa_Vouchers where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Vouchers_Detail_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Vouchers_Detail_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Vouchers_Detail')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Vouchers_Detail_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sNum, lOptID, sCardNum, sDeptNum, sBeforeVoucher, sAfterVoucher into @u8db..UFFA_BAK_fa_Vouchers_Detail_'+ltrim(str(@iMonth))+' from @u8db..fa_Vouchers_Detail where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Vouchers_Group_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Vouchers_Group_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Vouchers_Group')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Vouchers_Group_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Vouchers_Group_'+ltrim(str(@iMonth))+' from @u8db..fa_Vouchers_Group where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_Vouchers_GroupDetail_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_Vouchers_GroupDetail_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_Vouchers_GroupDetail')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_Vouchers_GroupDetail_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_Vouchers_GroupDetail_'+ltrim(str(@iMonth))+' from @u8db..fa_Vouchers_GroupDetail where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_WorkLoad_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_WorkLoad_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_WorkLoad')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_WorkLoad_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_WorkLoad_'+ltrim(str(@iMonth))+' from @u8db..fa_WorkLoad where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_WorkLoad_Detail_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_WorkLoad_Detail_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_WorkLoad_Detail')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_WorkLoad_Detail_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select sCardNum, sDeptNum, dblWork1, dblWorkT1, dblWork2, dblWorkT2, dblWork3, dblWorkT3, dblWork4, dblWorkT4, dblWork5, dblWorkT5, dblWork6, dblWorkT6, dblWork7, dblWorkT7, dblWork8, dblWorkT8, dblWork9, dblWorkT9, dblWork10, dblWorkT10, dblWork11, dblWorkT11, dblWork12, dblWorkT12, dblDeprWorkThisYear into @u8db..UFFA_BAK_fa_WorkLoad_Detail_'+ltrim(str(@iMonth))+' from @u8db..fa_WorkLoad_Detail where 1=0 '
			exec(@strsql)
		end
	end
	

	if exists (select 1 from @u8db..sysobjects where name ='UFFA_BAK_fa_ZWVouchers_'+ltrim(str(@iMonth)))
	begin
		set @strSql='drop table @u8db..UFFA_BAK_fa_ZWVouchers_'+ltrim(str(@iMonth))
		exec(@strsql)
	end
	if exists (select 1 from @u8db..sysobjects where name ='fa_ZWVouchers')
	begin
		if exists (select 1 from @t6db..sysobjects where name ='UFFA_BAK_fa_ZWVouchers_'+ltrim(str(@iMonth)))
		begin
			set @strSql='select * into @u8db..UFFA_BAK_fa_ZWVouchers_'+ltrim(str(@iMonth))+' from @u8db..fa_ZWVouchers where 1=0 '
			exec(@strsql)
		end
	end	
	set @iMonth =@iMonth+1
end
end
go
--修补网银数据
--If COLUMNPROPERTY(OBJECT_ID('NB_PayAccountInfo'),'cBranchId','PRECISION') IS NULL
if not exists (select 1 from @u8db..syscolumns where name ='cCBBDepId' and id =(select id from @u8db..sysobjects where 
		name ='NB_PayAccountInfo' and XTYPE='U'))
	 exec (' ALTER TABLE @u8db..NB_PayAccountInfo add 
		cCBBDepId varchar(60) null ')
if not exists (select 1 from @u8db..syscolumns where name ='cBranchId' and id =(select id from @u8db..sysobjects where 
		name ='NB_PayAccountInfo' and XTYPE='U'))		
	 exec (' ALTER TABLE @u8db..NB_PayAccountInfo add 
		cBranchId varchar(60) null ')
--集团账号
if  exists (select 1 from @t6db..syscolumns where name ='cAccountGroup' and id =(select id from @t6db..sysobjects where 
		name ='NB_payment' and XTYPE='U'))		
	exec ('update 	@t6db..NB_payment set cAccountGroup=null')


--单据表增加表示支付状态的字段2005-6-25

	exec ('ALTER TABLE @u8db..NB_Payment add iBillStatus int NULL ')
	exec ('ALTER TABLE @u8db..NB_Payment add cErrInfoRet nvarchar (120) NULL ')
GO

-- 网银单据状态(ibillstatus)在T6和U8中含义不一致:
IF EXISTS(Select bpayed From @t6db..NB_payment)	
	update @t6db..NB_payment 
		set iBillStatus = (CASE WHEN ISNULL(bpayed,0) = 0 THEN
				(CASE WHEN ISNULL(iBillStatus,-1) = -1 THEN 0
				 WHEN ISNULL(iBillStatus,-1) = 0 THEN 1
				WHEN ISNULL(iBillStatus,-1) = 1 THEN 3
				WHEN ISNULL(iBillStatus,-1) = 2 THEN 2
				ELSE 0 END)
				WHEN ISNULL(bpayed,0) = 1 THEN
					(CASE WHEN ISNULL(iBillStatus,-1) = 0 THEN 4
					 WHEN ISNULL(iBillStatus,-1) = 1 THEN 6
					WHEN ISNULL(iBillStatus,-1) = 2 THEN 5
					ELSE 0 END)
				END)
GO

-- 修补izdtype = 9摘要数据
IF NOT EXISTS(Select ID From @t6db..ap_digset where cFlag = 'AR' and izdtype = 9)
	insert into @t6db..ap_digset 
	SELECT * from @u8db..ap_digset where cFlag = 'AR' and izdtype = 9

IF NOT EXISTS(Select ID From @t6db..ap_digset where cFlag = 'AP' and izdtype = 9)
	insert into @t6db..ap_digset 
	SELECT * from @u8db..ap_digset where cFlag = 'AP' and izdtype = 9
go
--避免错误脚本执行(SQLFILE8600\Main\Ufdata\DPRD_GL.sql)
--"名为 'ItemClass' 的游标不存在"
update @t6db..fitemstructure set iLength=60 Where cField_name='citemname'
if not exists(select 1 from @t6db..fitemstructure where cField_name='citemname')
insert into @t6db..fitemstructure(citem_class,citem_sqr,cfield_name,ctext,imode,itype,ilength,iscale,blist,bsum,bref,isubitem,bprimarykey,borderby ) 
values(N'98',3,N'citemname',N'项目名称',2,3,60,0,1,0,1,0,0,1)
go