Transact-SQL Insert post inventory data in related ledgers
This post is about posting only new data from inventory table into vendor ledgers and material control account by automatically checking exisiting data. We are using true example from our accounting database. Transactionz table is a financial transactions table resulting from material received or returned. In inventory table data is recorded with date, material id, quantity received and vendor id.
This data is posted into two tables with transact-sql statements below statement 1 post the data into transactionz table as mentioned above and statement 2 post the same transaction into material ledger a/c after checking exisiting transaction ids by using ‘where not (tr_id = any (select trn from transactionz))’ so no duplicate transaction is posted as well if priceis 0 in case rate not finalized no transcation will be posted into ledgers though quantity is already received.
These statements made accounting entry very simple with 1 step i.e. once data is recorded in inventory it is automatically posted in effected ledger accounts accounts.
Statement-1
insert into transactionz (trn, transactiondate, transactiondescription, accountid, credit, debit)
select tr_id, transactiondate, cast(quantitycr as varchar)+ ‘ / ‘+ cast(quantity as varchar)+ ‘ ‘+ +cast([description/name] as nvarchar) + ‘ @ Rs. ‘ +cast(price as varchar) ,accountid, (quantity*price), (quantitycr*price) from view38
where not (tr_id = any (select trn from transactionz) and (price<> 0)
Statement-2
insert into transactionmat (trn, transactiondate, transactiondescription, accountid, debit, credit)
select tr_id, transactiondate, cast(quantity as varchar)+ ‘ / ‘+cast(quantitycr as varchar)+ ‘ ‘+ +cast([description/name] as nvarchar) + ‘ @ Rs. ‘ +cast(price as varchar) ,215, (quantity*price), (quantitycr*price) from view38
where not (tr_id = any (select trn from transactionmat) and (price<> 0)