Skip to end of metadata
Go to start of metadata


create or alter procedure _as_ChangeAccountType
	@newAccountTypeId int
	,@AccountId bigint
AS
BEGIN
SET NOCOUNT ON;
/* usage:
	exec _as_ChangeAccountType @newAccountTypeId = 10, @AccountId = 7114;
	exec _as_ChangeAccountType @newAccountTypeId = 10, @AccountId = 7115;

	select * from _as_AccountType_ChangeLog l
		left join accounts a on a.accountlink = l.accountid
*/

declare @tblAccs table (accId bigint)
insert into @tblAccs (accId) values( @AccountId)

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = '_as_AccountType_ChangeLog')
	BEGIN
	PRINT 'Creating _as_AccountType_ChangeLog logging table';

	CREATE TABLE _as_AccountType_ChangeLog (
		Id bigint IDENTITY(1,1) primary key,
		ProcName varchar(200),
		CreateDate datetime not null default GETDATE(),
		OldAccountTypeId int,
		NewAccountTypeId int,
		AccountId bigint,
		UserName nvarchar(250)
		)
	END

update a
	set a.iaccounttype = @newAccountTypeId
	OUTPUT inserted.iAccountType, deleted.iAccountType, inserted.AccountLink, '_as_AccountType_ChangeLog', SUSER_NAME()
		INTO _as_AccountType_ChangeLog ( NewAccountTypeId, OldAccountTypeId, AccountId,ProcName,UserName)
from accounts a where accountlink in (select accId from @tblAccs)

update b set b.iAccBlncAccountType = a.iAccountType
--select * 
from _etblAccBlnc b
	left join Accounts a on b.iAccBlncAccountID = a.AccountLink
	inner join @tblAccs t on b.iAccBlncAccountID = t.accId

update b set b.iAccPrevAccountType = a.iAccountType
--select * 
from _etblAccPrev b
	left join Accounts a on b.iaccprevAccountId = a.AccountLink
	inner join @tblAccs t on b.iAccPrevAccountID = t.accId

update b set b.iBudgetAccountType = a.iAccountType
--select * 
from _etblBudgets b
	left join Accounts a on b.iBudgetAccountId = a.AccountLink
	inner join @tblAccs t on b.iBudgetAccountID = t.accId


END --sproc




  • No labels