我已经为账户管理的库存POS写了以下查询账户分类帐。这是获取任何帐户的帐户分类帐的好方法,或者有更好的方法来获取帐户分类帐。
任何专家建议请..... ??
当我们使用此查询同时获取所有帐户余额的分类帐时,这种方式是否可行。??
Select 'DV-'+Convert(Varchar,VoucherNo) As VoucherNo, DebitVouchersBody.AccountNo, AccountName, VoucherDate AS Date, DebitVouchersBody.Narration, Debit, 0.00 As Credit
From DebitVouchersBody
Join ChartOfAccounts ON DebitVouchersBody.AccountNo=ChartOfAccounts.AccountNo
Where DebitVouchersBody.AccountNo = '" & txtAccountId.Text & "'
Union All
Select 'CV-'+Convert(Varchar,VoucherNo) As VoucherNo, CreditVouchersBody.AccountNo, AccountName, VoucherDate AS Date, CreditVouchersBody.Narration, 0.00 As Debit, Credit
From CreditVouchersBody
Join ChartOfAccounts ON CreditVouchersBody.AccountNo=ChartOfAccounts.AccountNo
Where CreditVouchersBody.AccountNo = '" & txtAccountId.Text & "'
Union All
Select 'JV-'+Convert(Varchar,VoucherNo) As VoucherNo, JournalVouchersBody.AccountNo, AccountName, VoucherDate AS Date, JournalVouchersBody.Narration, Debit, Credit
From JournalVouchersBody
Join ChartOfAccounts ON JournalVouchersBody.AccountNo=ChartOfAccounts.AccountNo
Where JournalVouchersBody.AccountNo = '" & txtAccountId.Text & "'
Union All
Select 'CD-'+Convert(Varchar,VoucherNo) As VoucherNo, BankDeposit.BankAccountNo, AccountName, VoucherDate AS Date, BankDeposit.Narration, Amount As Debit, 0.00 As Credit
From BankDeposit Join ChartOfAccounts ON BankDeposit.BankAccountNo=ChartOfAccounts.AccountNo
Where BankDeposit.BankAccountNo = '" & txtAccountId.Text & "' AND IsCheque='False'
Union All
Select 'BD-'+Convert(Varchar,VoucherNo) As VoucherNo, BankDeposit.BankAccountNo, AccountName, VoucherDate AS Date, BankDeposit.Narration, Amount As Debit, 0.00 As Credit
From BankDeposit
Join ChartOfAccounts ON BankDeposit.BankAccountNo=ChartOfAccounts.AccountNo
Where BankDeposit.BankAccountNo = '" & txtAccountId.Text & "' AND IsCheque='True'
Union All
Select 'BD-'+Convert(Varchar,VoucherNo) As VoucherNo, BankDeposit.ReceivedFromAccountNo, AccountName, VoucherDate AS Date, BankDeposit.Narration, 0.00 As Debit, Amount As Credit
From BankDeposit Join ChartOfAccounts ON BankDeposit.ReceivedFromAccountNo=ChartOfAccounts.AccountNo
Where BankDeposit.ReceivedFromAccountNo = '" & txtAccountId.Text & "' AND IsCheque='True'
Union All
Select 'CT-'+Convert(Varchar,VoucherNo) As VoucherNo, BankCheques.BankAccountNo, AccountName, VoucherDate AS Date, BankCheques.Narration, Amount As Debit, 0.00 As Credit
From BankCheques Join ChartOfAccounts ON BankCheques.BankAccountNo=ChartOfAccounts.AccountNo
Where BankCheques.BankAccountNo = '" & txtAccountId.Text & "' AND IsOnline='False'
Union All
Select 'OT-'+Convert(Varchar,VoucherNo) As VoucherNo, BankCheques.AccountNoPayTo, AccountName, VoucherDate AS Date, BankCheques.Narration, 0.00 As Debit, Amount As Credit
From BankCheques
Join ChartOfAccounts ON BankCheques.AccountNoPayTo=ChartOfAccounts.AccountNo
Where BankCheques.AccountNoPayTo = '" & txtAccountId.Text & "' AND IsOnline='True'
Union All
Select 'SI-'+Convert(Varchar,Sales.SaleID) As VoucherNo, CustomerId As AccountNo, AccountName, Date, Sales.Narration, NetValue As Debit, PaidAmount As Credit
From Sales
Join ChartOfAccounts ON Sales.CustomerId=ChartOfAccounts.AccountNo
Where CustomerId= '" & txtAccountId.Text & "'
Union All
Select 'SR-'+Convert(Varchar,SaleReturns.SaleReturnID) As VoucherNo, CustomerId As AccountNo, AccountName, Date, SaleReturns.Narration, PaidAmount As Debit, NetValue As Credit
From SaleReturns
Join ChartOfAccounts ON SaleReturns.CustomerId=ChartOfAccounts.AccountNo
Where CustomerId= '" & txtAccountId.Text & "'
Union All
Select 'PI-'+Convert(Varchar,Purchases.PurchaseId) AS VoucherNo, VendorId As AccountNo, AccountName, Date, Purchases.Narration, AmountPaid AS Debit, NetValue AS Credit
From Purchases
Join ChartOfAccounts ON Purchases.VendorId=ChartOfAccounts.AccountNo
Where VendorId= '" & txtAccountId.Text & "'
Union All
Select 'PR-'+Convert(Varchar,PurReturns.PurReturnId) AS VoucherNo, VendorId As AccountNo, AccountName, Date, PurReturns.Narration, NetValue AS Debit, AmountPaid AS Credit
From PurReturns
Join ChartOfAccounts ON PurReturns.VendorId=ChartOfAccounts.AccountNo
Where VendorId= '" & txtAccountId.Text & "'
Union All
Select 'CC-'+Convert(Varchar,CExpiryClaims.ClaimID) As VoucherNo, CustomerId As AccountNo, AccountName, Date, NULL As Narration, 0.00 As Debit, TTLValue As Credit
From CExpiryClaims
Join ChartOfAccounts ON CExpiryClaims.CustomerId=ChartOfAccounts.AccountNo
Where CustomerId= '" & txtAccountId.Text & "'
Union All
Select 'CR-'+Convert(Varchar,CExpiryReplies.ReplyID) As VoucherNo, CustomerId As AccountNo, AccountName, CexpiryReplies.Date, NULL As Narration, (IsNull(CexpiryReplies.TTLValue,0))+(IsNull(RepliedAmount,0)) As Debit, 0.00 As Credit
From CExpiryReplies
Join CExpiryClaims On CExpiryClaims.ClaimID=CExpiryReplies.ReplyID
Join ChartOfAccounts ON CExpiryClaims.CustomerId=ChartOfAccounts.AccountNo
Where CustomerId= '" & txtAccountId.Text & "'
Union All
Select 'VC-'+Convert(Varchar,ExpiryClaims.ClaimID) As VoucherNo, VendorId As AccountNo, AccountName, Date, NULL As Narration, TTLValue As Debit, 0.00 As Credit
From ExpiryClaims
Join ChartOfAccounts ON ExpiryClaims.VendorId=ChartOfAccounts.AccountNo
Where VendorId= '" & txtAccountId.Text & "'
Union All
Select 'CR-'+Convert(Varchar,ExpiryReplies.ReplyID) As VoucherNo, VendorId As AccountNo, AccountName, ExpiryReplies.Date, NULL As Narration, 0.00 As Debit, (IsNull(expiryReplies.TTLValue,0))+(IsNull(RepliedAmount,0)) As Credit
From ExpiryReplies
Join ExpiryClaims On ExpiryClaims.ClaimID=ExpiryReplies.ReplyID
Join ChartOfAccounts ON ExpiryClaims.VendorId=ChartOfAccounts.AccountNo
Where VendorId= '" & txtAccountId.Text & "'
Union All
Select 'RI-'+Convert(Varchar,RecoveryBodyInvoice.RecoveryID) As VoucherNo, CustomerId As AccountNo, AccountName, RecoveryDate, REcoveryBodyInvoice.Narration, 0.00 As Debit, Amount As Credit
From RecoveryBodyInvoice
Join Recovery ON RecoveryBodyInvoice.RecoveryID = Recovery.RecoveryID
join sales ON RecoveryBodyInvoice.SaleID=sales.SaleId
Join ChartOfAccounts ON Sales.CustomerId=ChartOfAccounts.AccountNo
Where CustomerId= '" & txtAccountId.Text & "'
Union All
Select 'RR-'+Convert(Varchar,RecoveryBodyWOInvoice.RecoveryID) As VoucherNo, CustomerId As AccountNo, AccountName, RecoveryDate, REcoveryBodyWOInvoice.Narration, 0.00 As Debit, Amount As Credit
From RecoveryBodyWOInvoice
Join Recovery ON RecoveryBodyWOInvoice.RecoveryID = Recovery.RecoveryID
Join ChartOfAccounts ON CustomerId=ChartOfAccounts.AccountNo
Where CustomerId= '" & txtAccountId.Text & "'
Union All
Select Null AS VoucherNo, Null As AccountNo, NULL As Name,Null As Date,'Opening Balance' AS Narration, OpeningDebit AS Debit, OpeningCredit As Crdit
From ChartOfAccounts
Where AccountNo = '" & txtAccountId.Text & "'
Order By VoucherDate, VoucherNo
答案 0 :(得分:1)
"好方法"需要了解你如何确定"好"与#34;坏"。阅读您的问题的任何人都不了解您的架构或目标 - 评估查询正确性或效率所需的内容。
你的查询是"坏"因为它很容易被sql注入 - 所以在你被黑客攻击之前去读一下。
您将数字转换为varchar但省略了长度 - 这是一种不好的做法。
您的查询会从给定帐户的开始时间开始选择所有活动。这样的结果集通常不太可能有用。
您的最后一个选择语句(期初余额)不包括任何日期(从何时开始?是的,可能有实际日期!),也不包括任何识别属性,如帐号ID,姓名等。这与其他日期不一致 - 让报告管理数据显示。
您将两种不同的信息组合到voucherno中。这是不好的做法。如果您认为这非常有用,那么您可能希望对已转换的ID值进行零填充,以便您可以排除"排队"实际报告中的值。
您的参数用法不一致,这使您的逻辑更难理解。尽管我可以告诉(tldr),但您总是将参数与chartofaccounts.accountno进行比较。实际上,在where子句中执行此操作以突出显示模式中的奇怪关系(其中accountno是customerid,vendorid等)。
最后 - 始终为您的表有用别名提供,并使用适当的别名引用所有列 - 以便读者知道哪个表是任何特定列的来源。并且您应该始终引用具有适当模式名称的表。