Seems 9.1 PL11 has the same/similar issue...
_FU_P object error during upgrade | SCN suggested a collation error
Upgrade 8.81 Customer DB error -  Object _... | SCN suggested removing custom views
Error on Upgrade 882 from PL04 to PL14 | SCN talks about the `Database 'FLNUOMTEST' is already open and can only have one user at a time.` error, but the fix is vague. The installer complains about some connections to the databases, and makes you get rid of them... So why it falls over this later is confusing
26/02/2016 17:58:40:427568 Sql Err I Tec DBMCSqlStatement::ExecDirect SQLExecDirect failed. Error: -1, Query: Create Procedure TmSp_GLAccountMigration
With Encryption
AS
/*****************************************************************************/
/* Account Migration when applying the new G/L Account Determination Matrix. */
/*****************************************************************************/
/* Drop function dbo.TmSp_GLAccountMigration_Replace_Account if already existed. */
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.TmSp_GLAccountMigration_Replace_Account')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION dbo.TmSp_GLAccountMigration_Replace_Account
/* Create function dbo.TmSp_GLAccountMigration_Replace_Account. */
declare @sqlCreateFunction nvarchar(max)
set @sqlCreateFunction = 'CREATE FUNCTION dbo.TmSp_GLAccountMigration_Replace_Account (@accountLevel nvarchar(max), @accountCompany nvarchar(max))
RETURNS nvarchar(max) WITH EXECUTE AS CALLER AS BEGIN DECLARE @acctCode nvarchar(max) IF (DATALENGTH(ltrim(rtrim(@accountLevel))) <> DATALENGTH(ltrim(rtrim(@accountCompany)))) SET @acctCode = @accountLevel ELSE SET @acctCode = REPLACE(@accountLevel , ISNULL(@accountCompany, ''''), '''') RETURN(@acctCode) END'
exec(@sqlCreateFunction)
/* Declare variables. */
declare @db sysname
declare @countOfByWHS int, @countOfByIG int, @countOfByITEM int, @recordOfWHS int, @recordOfIG int, @recordOfITEM int, @returnedCount int
declare @sql nvarchar(max), @sql1 nvarchar(max), @sql2 nvarchar(max), @accountLevel nvarchar(max), @accountCompany nvarchar(max), @lastAcct nvarchar(max)
declare @periodCat nvarchar(max), @financYear nvarchar(max), @year nvarchar(max), @periodName nvarchar(max),
@subType nvarchar(max), @periodNum nvarchar(max), @f_RefDate nvarchar(max), @t_RefDate nvarchar(max), @f_DueDate nvarchar(max), @t_DueDate nvarchar(max), @f_TaxDate nvarchar(max), @t_TaxDate nvarchar(max)
/* Prepare temp table #OGAR to save temp result */
if exists (select *
from tempdb..sysobjects where name like '#OGAR%') drop table #OGAR
/* Create table #OGAR - all fields are allowed to be NULL */
create table #OGAR(
[AbsEntry] [int] NULL, [PeriodCat] [nvarchar](10) NULL, [FinancYear] [datetime] NULL, [Year] [smallint] NULL, [PeriodName] [nvarchar](20) NULL, [SubType] [char](1) NULL, [PeriodNum] [int] NULL, [F_RefDate] [datetime] NULL, [T_RefDate] [datetime] NULL, [F_DueDate] [datetime] NULL, [T_DueDate] [datetime] NULL, [F_TaxDate] [datetime] NULL, [T_TaxDate] [datetime] NULL, [LogInstanc] [smallint] NULL, [UpdateDate] [datetime] NULL, [UserSign] [smallint] NULL, [UserSign2] [smallint] NULL, [ItemCode] [nvarchar](20) NULL, [ItmsGrpCod] [smallint] NULL, [WhsCode] [nvarchar](8) NULL, [BPGrpCod] [smallint] NULL, [LicTradNum] [nvarchar](32) NULL, [ShipCountr] [nvarchar](3) NULL, [ShipState] [nvarchar](3) NULL, [Comments] [nvarchar](254) NULL, [CreateDate] [datetime] NULL, [RuleCode] [nvarchar](20) NULL, [GLMethod] [char](1) NULL, /* Case V: C - Group by ItemGroup; L - Group by ItemLevel; W - Group by Warehouse; Otherwise: A - General. */ [Transfered] [char](1) NULL, [FromDate] [datetime] NULL, [ToDate] [datetime] NULL, [DfltExpn] [nvarchar](15) NULL, [DfltIncom] [nvarchar](15) NULL, [ExmptIncom] [nvarchar](15) NULL, [StockAct] [nvarchar](15) NULL, [COGM_Act] [nvarchar](15) NULL, [AlocCstAct] [nvarchar](15) NULL, [VariancAct] [nvarchar](15) NULL, [PricDifAct] [nvarchar](15) NULL, [NegStckAct] [nvarchar](15) NULL, [DfltLoss] [nvarchar](15) NULL, [DfltProfit] [nvarchar](15) NULL, [RturnngAct] [nvarchar](15) NULL, [ECIncome] [nvarchar](15) NULL, [ECExepnses] [nvarchar](15) NULL, [ForgnIncm] [nvarchar](15) NULL, [ForgnExpn] [nvarchar](15) NULL, [PurchseAct] [nvarchar](15) NULL, [PaReturnAc] [nvarchar](15) NULL, [PaOffsetAc] [nvarchar](15) NULL, [ExDiffAct] [nvarchar](15) NULL, [BalanceAct] [nvarchar](15) NULL, [DecresGlAc] [nvarchar](15) NULL, [IncresGlAc] [nvarchar](15) NULL, [WipAcct] [nvarchar](15) NULL, [WipVarAcct] [nvarchar](15) NULL, [WipOffset] [nvarchar](15) NULL, [StockOffst] [nvarchar](15) NULL, [StockRvAct] [nvarchar](15) NULL, [StkRvOfAct] [nvarchar](15) NULL, [CostRevAct] [nvarchar](15) NULL, [CostOffAct] [nvarchar](15) NULL, [ExpClrAct] [nvarchar](15) NULL, [ExpOfstAct] [nvarchar](15) NULL, [StkInTnAct] [nvarchar](15) NULL, [ShpdGdsAct] [nvarchar](15) NULL, [VatRevAct] [nvarchar](15) NULL, [ARCMAct] [nvarchar](15) NULL, [APCMAct] [nvarchar](15) NULL, [ARCMExpAct] [nvarchar](15) NULL, [ARCMFrnAct] [nvarchar](15) NULL, [APCMFrnAct] [nvarchar](15) NULL, [ARCMEUAct] [nvarchar](15) NULL, [APCMEUAct] [nvarchar](15) NULL, [PurBalAct] [nvarchar](15) NULL, [WhICenAct] [nvarchar](15) NULL, [WhOCenAct] [nvarchar](15) NULL, [Active] [char](1) NULL, [CmpPrivate] [char](1) NULL, [VatGroup] [nvarchar](8) NULL, [CardCode] [nvarchar](15) NULL, [Usage] [int] NULL, [FreeChrgSA] [nvarchar](15) NULL, [FreeChrgPU] [nvarchar](15) NULL) delete from #OGAR
/* Before migration, delete all records from table OGAR */
delete from OGAR
/* prepare data to be saved. */
select @countOfByWHS = COUNT(1) from OITM where GLMethod = 'W'
select @countOfByIG = COUNT(1) from OITM where GLMethod = 'C'
select @countOfByITEM = COUNT(1) from OITM where GLMethod = 'L'
/* Fields mapping between OWHS/OITB/OITW and OACP.
w - field alias in OWHS/OITB/OITW
a - field alias in OACP
l - last account? */
declare @FieldMap table (w nvarchar(max), a nvarchar(max), l nvarchar(max))
insert into @FieldMap
select 'ExpensesAc', 'DfltExpn', 'N' union all select 'RevenuesAc', 'DfltIncom', 'N' union all select 'ExmptIncom', 'ExmptIncom', 'N' union all select 'BalInvntAc', 'StockAct', 'N' union all select 'SaleCostAc', 'COGM_Act', 'N' union all select 'TransferAc', 'AlocCstAct', 'N' union all select 'VarianceAc', 'VariancAct', 'N' union all select 'PriceDifAc', 'PricDifAct', 'N' union all select 'NegStckAct', 'NegStckAct', 'N' union all select 'DecreasAc', 'DfltLoss', 'N' union all select 'IncreasAc', 'DfltProfit', 'N' union all select 'ReturnAc', 'RturnngAct', 'N' union all select 'EURevenuAc', 'ECIncome', 'N' union all select 'EUExpensAc', 'ECExepnses', 'N' union all select 'FrRevenuAc', 'ForgnIncm', 'N' union all select 'FrExpensAc', 'ForgnExpn', 'N' union all select 'PurchaseAc', 'PurchseAct', 'N' union all select 'PAReturnAc', 'PaReturnAc', 'N' union all select 'PurchOfsAc', 'PaOffsetAc', 'N' union all select 'ExchangeAc', 'ExDiffAct', 'N' union all select 'BalanceAcc', 'BalanceAct', 'N' union all select 'DecresGlAc', 'DecresGlAc', 'N' union all select 'IncresGlAc', 'IncresGlAc', 'N' union all select 'WipAcct', 'WipAcct', 'N' union all select 'WipVarAcct', 'WipVarAcct', 'N' union all select 'WipOffset', 'WipOffset', 'N' union all select 'StockOffst', 'StockOffst', 'N' union all select 'StokRvlAct', 'StockRvAct', 'N' union all select 'StkOffsAct', 'StkRvOfAct', 'N' union all select 'CostRvlAct', 'CostRevAct', 'N' union all select 'CstOffsAct', 'CostOffAct', 'N' union all select 'ExpClrAct', 'ExpClrAct', 'N' union all select 'ExpOfstAct', 'ExpOfstAct', 'N' union all select 'StkInTnAct', 'StkInTnAct', 'N' union all select 'ShpdGdsAct', 'ShpdGdsAct', 'N' union all select 'VatRevAct', 'VatRevAct', 'N' union all select 'ARCMAct', 'ARCMAct', 'N' union all select 'APCMAct', 'APCMAct', 'N' union all select 'ARCMExpAct', 'ARCMExpAct', 'N' union all select 'ARCMFrnAct', 'ARCMFrnAct', 'N' union all select 'APCMFrnAct', 'APCMFrnAct', 'N' union all select 'ARCMEUAct', 'ARCMEUAct', 'N' union all select 'APCMEUAct', 'APCMEUAct', 'N' union all select 'PurBalAct', 'PurBalAct', 'N' union all select 'WhICenAct', 'WhICenAct', 'N' union all select 'WhOCenAct', 'WhOCenAct', 'Y'
/*
Case I: If all value are 'W' which means all items of the company were set G/L account by Warehouse
1. Take all types of the accounts from warehouses whose values are different from company level. */
if @countOfByWHS > 0 and @countOfByIG + @countOfByITEM = 0
begin declare db_cursor1 CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor1 FETCH NEXT FROM db_cursor1 INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, ''!^|'', ''-1'', OWHS.WhsCode, ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL' /* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OWHS.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OWHS.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OWHS.' + @accountLevel + ' != '''' and OWHS.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor1 INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OWHS inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql)
close db_cursor1 deallocate db_cursor1 end
/*
Case II : If all value are 'C', which means all items of the company were set G/L account by Item Group
1. Take all types of the accounts from itemgroups whose values are different from company level. */
else if @countOfByIG > 0 and @countOfByWHS+@countOfByITEM = 0
begin declare db_cursor2 CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor2 FETCH NEXT FROM db_cursor2 INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, ''!^|'', OITB.ItmsGrpCod, ''!^|'', ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL'
/* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OITB.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OITB.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OITB.' + @accountLevel + ' != '''' and OITB.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor2 INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OITB inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql) close db_cursor2 deallocate db_cursor2 end
/*
Case III : If all value are 'W' or 'L', which means all items were set by Warehouse or by item
1. Take all types of the accounts from warehouses whose values are different from company level. 2. Take all types of the accounts from table OITW whose values are different from warehouses. */
else if @countOfByWHS+@countOfByITEM > 0 and @countOfByIG = 0
begin declare db_cursor3X CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor3X FETCH NEXT FROM db_cursor3X INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, ''!^|'', ''-1'', OWHS.WhsCode, ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL' /* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OWHS.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OWHS.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OWHS.' + @accountLevel + ' != '''' and OWHS.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor3X INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OWHS inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql) close db_cursor3X deallocate db_cursor3X /*--------------------------------------------------------------------------------------------------*/ declare db_cursor3 CURSOR FOR select OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1 order by OACP.PeriodCat
OPEN db_cursor3 FETCH NEXT FROM db_cursor3 INTO @periodCat, @financYear, @year, @periodName, @subType, @periodNum, @f_RefDate, @t_RefDate, @f_DueDate, @t_DueDate, @f_TaxDate, @t_TaxDate
/* Loop all posting periods */ WHILE @@FETCH_STATUS = 0 begin declare db_cursor3Y CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor3Y FETCH NEXT FROM db_cursor3Y INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, OITW.ItemCode, OITM.ItmsGrpCod, OITW.WhsCode, ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL'
/* 46 accounts selection */ set @sql1 = ''
/* 46 accounts condition */ set @sql2 = '' /* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OITW.' + @accountLevel + ', ISNULL(OWHS.' + @accountLevel + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OITW.' + @accountLevel + ' != OWHS.' + @accountLevel + ' or OWHS.' + @accountLevel + ' IS NULL' + ' or OWHS.' + @accountLevel + ' = '''') and OITW.' + @accountLevel + ' != '''' and OITW.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor3Y INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OITW inner join OITM on OITW.itemcode = OITM.itemcode inner join OWHS on OITW.WhsCode = OWHS.WhsCode where OITM.GLMethod = ''L'' and ' + @sql2
/* Execute SQL */ exec(@sql)
/* Fill values for PeriodCat, FinancYear ... */ update #OGAR set PeriodCat = @periodCat where PeriodCat = '' or PeriodCat IS NULL update #OGAR set FinancYear = @financYear where PeriodCat = @periodCat and (FinancYear = '' or FinancYear IS NULL) update #OGAR set Year = @year where PeriodCat = @periodCat and (Year = '' or Year IS NULL) update #OGAR set PeriodName = @periodName where PeriodCat = @periodCat and (PeriodName = '' or PeriodName IS NULL) update #OGAR set SubType = @subType where PeriodCat = @periodCat and (SubType = '' or SubType IS NULL) update #OGAR set PeriodNum = @periodNum where PeriodCat = @periodCat and (PeriodNum = '' or PeriodNum IS NULL) update #OGAR set F_RefDate = @f_RefDate where PeriodCat = @periodCat and (F_RefDate = '' or F_RefDate IS NULL) update #OGAR set T_RefDate = @t_RefDate where PeriodCat = @periodCat and (T_RefDate = '' or T_RefDate IS NULL) update #OGAR set F_DueDate = @f_DueDate where PeriodCat = @periodCat and (F_DueDate = '' or F_DueDate IS NULL) update #OGAR set T_DueDate = @t_DueDate where PeriodCat = @periodCat and (T_DueDate = '' or T_DueDate IS NULL) update #OGAR set F_TaxDate = @f_TaxDate where PeriodCat = @periodCat and (F_TaxDate = '' or F_TaxDate IS NULL) update #OGAR set T_TaxDate = @t_TaxDate where PeriodCat = @periodCat and (T_TaxDate = '' or T_TaxDate IS NULL) close db_cursor3Y deallocate db_cursor3Y
FETCH NEXT FROM db_cursor3 INTO @periodCat, @financYear, @year, @periodName, @subType, @periodNum, @f_RefDate, @t_RefDate, @f_DueDate, @t_DueDate, @f_TaxDate, @t_TaxDate end close db_cursor3 deallocate db_cursor3 end
/*
Case IV : If all value are 'C' or 'L', which means all items were set by Item Group or by item
1. Take all types of the accounts from itemgroups whose values are different from company level. 2. Take all types of the accounts from table OITW whose values are different from itemgroups. */
else if @countOfByIG+@countOfByITEM > 0 and @countOfByWHS = 0
begin declare db_cursor4X CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor4X FETCH NEXT FROM db_cursor4X INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, ''!^|'', OITB.ItmsGrpCod, ''!^|'', ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL'
/* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OITB.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OITB.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OITB.' + @accountLevel + ' != '''' and OITB.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor4X INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OITB inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql) close db_cursor4X deallocate db_cursor4X /*--------------------------------------------------------------------------------------------------*/ declare db_cursor4 CURSOR FOR select OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1 order by OACP.PeriodCat
OPEN db_cursor4 FETCH NEXT FROM db_cursor4 INTO @periodCat, @financYear, @year, @periodName, @subType, @periodNum, @f_RefDate, @t_RefDate, @f_DueDate, @t_DueDate, @f_TaxDate, @t_TaxDate
/* Loop all posting periods */ WHILE @@FETCH_STATUS = 0 begin declare db_cursor4Y CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor4Y FETCH NEXT FROM db_cursor4Y INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, OITW.ItemCode, OITM.ItmsGrpCod, OITW.WhsCode, ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL'
/* 46 accounts selection */ set @sql1 = ''
/* 46 accounts condition */ set @sql2 = '' /* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OITW.' + @accountLevel + ', ISNULL(OITB.' + @accountLevel + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OITW.' + @accountLevel + ' != OITB.' + @accountLevel + ' or OITB.' + @accountLevel + ' IS NULL' + ' or OITB.' + @accountLevel + ' = '''') and OITW.' + @accountLevel + ' != '''' and OITW.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor4Y INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OITW inner join OITM on OITW.itemcode = OITM.itemcode inner join OITB on OITB.ItmsGrpCod = OITM.ItmsGrpCod where OITM.GLMethod = ''L'' and ' + @sql2
/* Execute SQL */ exec(@sql)
/* Fill values for PeriodCat, FinancYear ... */ update #OGAR set PeriodCat = @periodCat where PeriodCat = '' or PeriodCat IS NULL update #OGAR set FinancYear = @financYear where PeriodCat = @periodCat and (FinancYear = '' or FinancYear IS NULL) update #OGAR set Year = @year where PeriodCat = @periodCat and (Year = '' or Year IS NULL) update #OGAR set PeriodName = @periodName where PeriodCat = @periodCat and (PeriodName = '' or PeriodName IS NULL) update #OGAR set SubType = @subType where PeriodCat = @periodCat and (SubType = '' or SubType IS NULL) update #OGAR set PeriodNum = @periodNum where PeriodCat = @periodCat and (PeriodNum = '' or PeriodNum IS NULL) update #OGAR set F_RefDate = @f_RefDate where PeriodCat = @periodCat and (F_RefDate = '' or F_RefDate IS NULL) update #OGAR set T_RefDate = @t_RefDate where PeriodCat = @periodCat and (T_RefDate = '' or T_RefDate IS NULL) update #OGAR set F_DueDate = @f_DueDate where PeriodCat = @periodCat and (F_DueDate = '' or F_DueDate IS NULL) update #OGAR set T_DueDate = @t_DueDate where PeriodCat = @periodCat and (T_DueDate = '' or T_DueDate IS NULL) update #OGAR set F_TaxDate = @f_TaxDate where PeriodCat = @periodCat and (F_TaxDate = '' or F_TaxDate IS NULL) update #OGAR set T_TaxDate = @t_TaxDate where PeriodCat = @periodCat and (T_TaxDate = '' or T_TaxDate IS NULL) close db_cursor4Y deallocate db_cursor4Y
FETCH NEXT FROM db_cursor4 INTO @periodCat, @financYear, @year, @periodName, @subType, @periodNum, @f_RefDate, @t_RefDate, @f_DueDate, @t_DueDate, @f_TaxDate, @t_TaxDate end close db_cursor4 deallocate db_cursor4 end
/*
Case V : If all value are 'C' or 'W', or all value are 'C' or 'W' or 'L'
1. Take all types of the accounts from warehouses whose values are different from company level. 2. Take all types of the accounts from itemgroups whose values are different from company level. 3. Take all types of the accounts from table OITW whose values are different from company level - we have taken the flag together with the rules, so compare to the company level instead of itemgroups/warehouses. */
else if @countOfByWHS > 0 and @countOfByIG > 0
begin declare db_cursor5X CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor5X FETCH NEXT FROM db_cursor5X INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, ''!^|'', ''-1'', OWHS.WhsCode, ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''W'', ''N'', NULL, NULL' /* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OWHS.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OWHS.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OWHS.' + @accountLevel + ' != '''' and OWHS.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor5X INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OWHS inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql) close db_cursor5X deallocate db_cursor5X /*--------------------------------------------------------------------------------------------------*/ declare db_cursor5Y CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor5Y FETCH NEXT FROM db_cursor5Y INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, ''!^|'', OITB.ItmsGrpCod, ''!^|'', ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''C'', ''N'', NULL, NULL'
/* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OITB.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OITB.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OITB.' + @accountLevel + ' != '''' and OITB.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor5Y INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OITB inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql) close db_cursor5Y deallocate db_cursor5Y /*--------------------------------------------------------------------------------------------------*/
declare db_cursor5Z CURSOR FOR SELECT w, a, l from @FieldMap OPEN db_cursor5Z FETCH NEXT FROM db_cursor5Z INTO @accountLevel, @accountCompany, @lastAcct
/* Overall query SQL */ set @sql = 'insert into #OGAR select NULL, OACP.PeriodCat, OACP.FinancYear, OACP.Year, OACP.PeriodName, OACP.SubType, OACP.PeriodNum, OACP.F_RefDate, OACP.T_RefDate, OACP.F_DueDate, OACP.T_DueDate, OACP.F_TaxDate, OACP.T_TaxDate, NULL, NULL, NULL, NULL, OITW.ItemCode, OITM.ItmsGrpCod, OITW.WhsCode, ''-1'', ''!^|'', ''!^|'', ''!^|'', NULL, NULL, NULL, ''A'', ''N'', NULL, NULL'
/* 46 accounts selection */ set @sql1 = '' /* 46 accounts condition */ set @sql2 = ''
/* Loop 46 accounts */ WHILE @@FETCH_STATUS = 0 begin /* build selection */ set @sql1 = @sql1 + ', dbo.TmSp_GLAccountMigration_Replace_Account(OITW.' + @accountLevel + ', ISNULL(OACP.' + @accountCompany + ', ''''))' /* build condition */ set @sql2 = @sql2 + '((OITW.' + @accountLevel + ' != OACP.' + @accountCompany + ' or OACP.' + @accountCompany + ' IS NULL' + ' or OACP.' + @accountCompany + ' = '''') and OITW.' + @accountLevel + ' != '''' and OITW.' + @accountLevel + ' IS NOT NULL)' if (@lastAcct = 'N') begin set @sql2 = @sql2 + ' or ' end FETCH NEXT FROM db_cursor5Z INTO @accountLevel, @accountCompany, @lastAcct end
/* Build overall query SQL */ set @sql = @sql + @sql1 + ', ''Y'', ''A'', ''!^|'', ''!^|'', ''-1'', NULL, NULL from OITW inner join OITM on OITW.itemcode = OITM.itemcode inner join (select OACP.* from OACP where (select COUNT(*) from OFPR where OACP.PeriodCat = OFPR.Category) >= 1) as OACP on 1 = 1 where OITM.GLMethod = ''L'' and ' + @sql2
/* Add order by in query SQL */ set @sql = @sql + ' order by PeriodCat'
/* Execute SQL */ exec(@sql) close db_cursor5Z deallocate db_cursor5Z end
/* update absentry for temp table #OGAR */
declare @absEntry int, @ruleCodeIndex int, @periodCatTmp nvarchar(max), @ruleCodeTmp nvarchar(max)
/* to get the first available absentry from table onnm */
select @absEntry = AutoKey from ONNM where objectcode = '1470000057' /* value of object GAR is 1470000057 */
DECLARE db_cursorM CURSOR FOR
SELECT distinct PeriodCat FROM #OGAR
OPEN db_cursorM
FETCH NEXT FROM db_cursorM INTO @periodCatTmp
WHILE @@FETCH_STATUS = 0
begin DECLARE db_cursorN CURSOR FOR SELECT RuleCode FROM #OGAR where PeriodCat = @periodCatTmp OPEN db_cursorN FETCH NEXT FROM db_cursorN INTO @ruleCodeTmp set @ruleCodeIndex = 1 WHILE @@FETCH_STATUS = 0 begin UPDATE #OGAR SET AbsEntry = @absEntry, RuleCode = 'R'+CAST(@ruleCodeIndex AS nvarchar(max)) WHERE CURRENT OF db_cursorN set @absEntry = @absEntry + 1 set @ruleCodeIndex = @ruleCodeIndex + 1 FETCH NEXT FROM db_cursorN INTO @ruleCodeTmp end close db_cursorN deallocate db_cursorN FETCH NEXT FROM db_cursorM INTO @periodCatTmp end close db_cursorM
deallocate db_cursorM
/* Move all the records from #OGAR to OGAR, and delete from #OGAR */
insert into OGAR select * from #OGAR
delete from #OGAR
/* update AutoKey in ONNM table */
update onnm set AutoKey = @absEntry where objectcode = '1470000057' /* value of object GAR is 1470000057 */
/* Drop table #OGAR at the end*/
delete from #OGAR
drop table #OGAR
/* In Case V: GLPickMeth should equal to GLMethod in OITM when it is set as ByWHS/ByIG */
if @countOfByIG > 0 and @countOfByWHS > 0
begin update OITM set GLPickMeth = GLMethod where GLMethod = 'W' or GLMethod = 'C' update OITM set GLPickMeth = 'A' where GLMethod = 'L' end, sqlState: 42000, nativeErr: 924, errText:[Microsoft][SQL Server Native Client 11.0][SQL Server]Database 'FLNUOMTEST' is already open and can only have one user at a time. # # TID=3212 __DBMC_Statement.cpp 222 Customized=0