2017-10-23 11 views
-1

勘定科目管理を使用して在庫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 
+2

ボビー・テーブルが来る前に、パラメータ化されたクエリを読んで理解し、使い始める必要があります。 http://bobby-tables.com/ –

答えて

1

「良い方法」には、「良い」と「悪い」を判断する方法が必要です。また、あなたの質問を読んだ人の誰も、スキーマや目標の正確な知識を持っていません。クエリや効率の正確性を評価するために必要なものです。

あなたのクエリはSQLインジェクションの影響を受けやすいという点で「悪い」ので、ハッキングする前にそのことをお読みください。

数値をvarcharに変換しますが、長さは省略します。これは悪い習慣です。

このクエリは、特定のアカウントの開始日以降のすべてのアクティビティを選択します。そのような結果セットは一般的には有用ではありません。

あなたの最後の選択ステートメント(期首残高)には日付は含まれていません(実際の日付がある可能性がありますので開いています)。アカウントID、名前などの識別属性もありません。その他 - レポートにデータの表示を管理させる。

2つの異なる情報を組み合わせて、バウチャーに入れます。それは悪い習慣です。これが本当に便利だと思われる場合は、変換されたID値をゼロにして、実際のレポートの値を「並べ替える」ことができます。

パラメータの使用が矛盾しているため、ロジックがわかりにくくなります。私が知る限り(tldr)、実際には常にパラメータをchartofaccounts.accountnoと比較します。だから、あなたのスキーマ(accountnoはcustomerid、vendoridなど)の奇妙な関係を強調するためにwhere句で実際にそれを行います。

そして最後に - 読者は、特定の列のソースであるテーブルを知っているように - 常にあなたのテーブル便利エイリアスと参照適切なエイリアスを持つすべての列を与えます。また、適切なスキーマ名を持つ表も常に参照する必要があります。

関連する問題