This is my actual stored procedure. As you can see, its too long and i am looking for ways to make it shorter and efficient, thank you i really appreciate your help :)SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GO--SAMPLE: usp_t_transaction_listByCustomer 'eb5007f0-88c9-4758-83df-5bf2c831dc14','','',''ALTER PROC dbo.usp_t_transaction_listByCustomer @fxKeyCustomer uniqueidentifier, @fcType varchar(50) = '', @fdDateFrom SMALLDATETIME = NULL, @fdDateTo SMALLDATETIME = NULLASIF @fdDateTo IS NULL SET @fdDateTo=@fdDateFromIF @fcType <> ''BEGIN IF @fcType = 'Sales Orders' BEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN-- PRINT 'NULL' SELECT fxKeySO as fxKey, 'tSalesOrder' as fcTable, 'fxKeySO' as fcCol, 'Sales Order' as fcType, CAST(fcSONo as nVarchar) as fcNo, fdDateTransact as fdDate, 'Sales Orders' as fcAccount, fdTotal as fdAmount FROM tSalesOrder WHERE fxKeyCustomer = @fxKeyCustomer ORDER BY fdDate END ELSE BEGIN-- PRINT 'NOT NULL' SELECT fxKeySO as fxKey, 'tSalesOrder' as fcTable, 'fxKeySO' as fcCol, 'Sales Order' as fcType, CAST(fcSONo as nVarchar) as fcNo, fdDateTransact AS fdDate, 'Sales Orders' as fcAccount, fdTotal as fdAmount FROM tSalesOrder WHERE fxKeyCustomer = @fxKeyCustomer AND fdDateTransact BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate END END ELSE IF @fcType = 'Invoices' BEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN SELECT fxKeyInvoice as fxKey, 'tInvoice' as fcTable, 'fxKeyInvoice' as fcCol, 'Invoice' as fcType, cast(fcInvoiceNo as nVarchar) as fcNo, fdDateTransact as fdDate, 'Accounts Receivable' as fcAccount, fdBalance as fdAmount FROM tInvoice WHERE fxKeyCustomer = @fxKeyCustomer AND fbCreditMemo = 0 ORDER BY fdDate END ELSE BEGIN SELECT fxKeyInvoice as fxKey, 'tInvoice' as fcTable, 'fxKeyInvoice' as fcCol, 'Invoice' as fcType, cast(fcInvoiceNo as nVarchar) as fcNo, fdDateTransact as fdDate, 'Accounts Receivable' as fcAccount, fdBalance as fdAmount FROM tInvoice WHERE fxKeyCustomer = @fxKeyCustomer AND fbCreditMemo = 0 AND fdDateTransact BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate END END ELSE IF @fcType = 'Credit Memos' BEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN SELECT fxKeyCredit as fxKey, 'tCreditMemo' as fcTable, 'fxKeyCredit' as fcCol, 'Credit Memo' as fcType, cast(fcCreditNo as nVarchar) as fcNo, fdTransDate as fdDate, '' as fcAccount, fdTotCredit as fdAmount FROM dbo.tCreditMemo WHERE fxKeyCustomer = @fxKeyCustomer AND fbCancelled = 0 ORDER BY fdDate END ELSE BEGIN SELECT fxKeyCredit as fxKey, 'tCreditMemo' as fcTable, 'fxKeyCredit' as fcCol, 'Credit Memo' as fcType, cast(fcCreditNo as nVarchar) as fcNo, fdTransDate as fdDate, '' as fcAccount, fdTotCredit as fdAmount FROM tCreditMemo WHERE fxKeyCustomer = @fxKeyCustomer AND fdTransDate BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate END END ELSE IF @fcType = 'Debit Memos' BEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN SELECT fxKeyDebit as fxKey, 'tDebitMemo' as fcTable, 'fxKeyDebit' as fcCol, 'Debit Memo' as fcType, cast(fcDebitNo as nVarchar) as fcNo, fdDateTrans as fdDate, '' as fcAccount, fdDBTol as fdAmount FROM dbo.tDebitMemo WHERE fxKeyName = @fxKeyCustomer ORDER BY fdDate END ELSE BEGIN SELECT fxKeyDebit as fxKey, 'tDebitMemo' as fcTable, 'fxKeyDebit' as fcCol, 'Debit Memo' as fcType, cast(fcDebitNo as nVarchar) as fcNo, fdDateTrans as fdDate, '' as fcAccount, fdDBTol as fdAmount FROM tDebitMemo WHERE fxKeyName = @fxKeyCustomer AND fdDateTrans BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate END END ELSE IF @fcType = 'Journal Entries' begin IF @fdDateFrom IS NULL OR @fdDateFrom = '' begin SELECT fxKeyJVNo as fxKey, 'tJVEntry' as fcTable, 'fxKeyJVNo' as fcCol, 'Journal Entry' as fcType, cast(right(left(fiEntryNo,6),4)+ right(left(fiEntryNo, 9),2)+ left(right(fiEntryNo, 8),2)+ right(fiEntryNo,5) as nVarchar) as fcNo, fdTransDate AS fdDate, 'Journal Entries' AS fcAccount, fdTotAmt as fdAmount FROM dbo.tJVEntry WHERE fxKeyNameID = @fxKeyCustomer ORDER BY fdDate end else begin select fxKeyJVNo as fxKey, 'tJVEntry' as fcTable, 'fxKeyJVNo' as fcCol, 'Journal Entry' as fcType, cast(right(left(fiEntryNo,6),4)+ right(left(fiEntryNo, 9),2)+ left(right(fiEntryNo, 8),2)+ right(fiEntryNo,5) as nVarchar) as fcNo, fdTransDate AS fdDate, 'Journal Entries' AS fcAccount, fdTotAmt AS fdAmount FROM dbo.tJVEntry WHERE fxKeyNameID = @fxKeyCustomer AND fdTransDate BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate end end ELSE IF @fcType = 'Sales Receipts' BEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN SELECT fxKeySR as fxKey, 'tSalesReceipt' as fcTable, 'fxKeySR' as fcCol, 'Sales Receipt' as fcType, cast(fcSaleNo as nVarchar) as fcNo, fdDateTransact AS fdDate, 'Undeposited Funds' AS fcAccount, fdBalanceDue AS fdAmount FROM tSalesReceipt WHERE fxKeyCustomer = @fxKeyCustomer ORDER BY fdDate END ELSE BEGIN SELECT fxKeySR as fxKey, 'tSalesReceipt' as fcTable, 'fxKeySR' as fcCol, 'Sales Receipt' as fcType, cast(fcSaleNo as nVarchar) as fcNo, fdDateTransact AS fdDate, 'Undeposited Funds' AS fcAccount, fdBalanceDue AS fdAmount FROM tSalesReceipt WHERE fxKeyCustomer = @fxKeyCustomer AND fdDateTransact BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate END END ELSE IF @fcType = 'Received Payments' BEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN SELECT fxKeyPayment as fxKey, 'tPayment' as fcTable, 'fxKeyPayment' as fcCol, 'Payment' as fcType, cast(fcRefNo as nVarchar) as fcNo, fdDate, 'Undeposited Funds' AS fcAccount, fdPayment AS fdAmount FROM tPayment WHERE fxKeyCustomer = @fxKeyCustomer ORDER BY fdDate END ELSE BEGIN SELECT fxKeyPayment as fxKey, 'tPayment' as fcTable, 'fxKeyPayment' as fcCol, 'Payment' as fcType, cast(fcRefNo as nVarchar) as fcNo, fdDate, 'Undeposited Funds'AS fcAccount, fdPayment AS fdAmount FROM tPayment WHERE fxKeyCustomer = @fxKeyCustomer AND fdDate BETWEEN @fdDateFrom AND @fdDateTo ORDER BY fdDate END ENDENDELSEBEGIN IF @fdDateFrom IS NULL OR @fdDateFrom = '' BEGIN SELECT fxKey, fcTable, fcCol, fcType, fcNo, fdDate, fcAccount, fdAmount FROM (SELECT fxKeyInvoice as fxKey, 'tInvoice' as fcTable, 'fxKeyInvoice' as fcCol, 'Invoice' as fcType, fcInvoiceNo as fcNo, fdDateTransact as fdDate, 'Accounts Receivable' as fcAccount, fdBalance as fdAmount FROM tInvoice WHERE fxKeyCustomer = @fxKeyCustomer AND fbCreditMemo = 0 UNION SELECT fxKeyCredit as fxKey, 'tCreditMemo' as fcTable, 'fxKeyCredit' as fcCol, 'Credit Memo' as fcType, fcCreditNo as fcNo, fdTransDate as fdDate, '' as fcAccount, fdTotCredit as fdAmount FROM dbo.tCreditMemo WHERE fxKeyCustomer = @fxKeyCustomer AND fbCancelled = 0 UNION SELECT fxKeyJVNo as fxKey, 'tJVEntry' as fcTable, 'fxKeyJVNo' as fcCol, 'Journal Entry' as fcType, cast(right(left(fiEntryNo,6),4)+ right(left(fiEntryNo, 9),2)+ left(right(fiEntryNo, 8),2)+ right(fiEntryNo,5) as nVarchar) as fcNo, fdTransDate as fdDate, 'Journal Entries' as fcAccount, fdTotAmt as fdAmount from dbo.tJVEntry where fxKeyNameID = @fxKeyCustomer UNION SELECT fxKeyDebit as fxKey, 'tDebitMemo' as fcTable, 'fxKeyDebit' as fcCol, 'Debit Memo' as fcType, fcDebitNo as fcNo, fdDateTrans as fdDate, '' as fcAccount, fdDBTol as fdAmount FROM dbo.tDebitMemo WHERE fxKeyName = @fxKeyCustomer UNION SELECT fxKeyPayment as fxKey, 'tPayment' as fcTable, 'fxKeyPayment' as fcCol, 'Payment' as fcType, fcRefNo as fcNo, fdDate, 'Undeposited Funds' as fcAccount, fdPayment as fdAmount FROM tPayment WHERE fxKeyCustomer = @fxKeyCustomer UNION SELECT fxKeySO as fxKey, 'tSalesOrder' as fcTable, 'fxKeySO' as fcCol, 'Sales Order' as fcType, fcSONo as fcNo, fdDateTransact AS fdDate, 'Sales Orders' as fcAccount, fdTotal as fdAmount FROM tSalesOrder WHERE fxKeyCustomer = @fxKeyCustomer UNION SELECT fxKeySR as fxKey, 'tSalesReceipt' as fcTable, 'fxKeySR' as fcCol, 'Sales Receipt' as fcType, fcSaleNo as fcNo, fdDateTransact AS fdDate, 'Undeposited Funds'as fcAccount, fdBalanceDue as fdAmount FROM tSalesReceipt WHERE fxKeyCustomer = @fxKeyCustomer )q ORDER by fcType, fdDate END ELSE BEGIN SELECT fxKey, fcTable, fcCol, fcType, fcNo, fdDate, fcAccount, fdAmount FROM (SELECT fxKeyInvoice as fxKey, 'tInvoice' as fcTable, 'fxKeyInvoice' as fcCol, 'Invoice' as fcType, fcInvoiceNo as fcNo, fdDateTransact as fdDate, 'Accounts Receivable' as fcAccount, fdBalance as fdAmount FROM tInvoice WHERE fxKeyCustomer = @fxKeyCustomer AND fbCreditMemo = 0 UNION SELECT fxKeyCredit as fxKey, 'tCreditMemo' as fcTable, 'fxKeyCredit' as fcCol, 'Credit Memo' as fcType, fcCreditNo as fcNo, fdTransDate as fdDate, '' as fcAccount, fdTotCredit as fdAmount FROM dbo.tCreditMemo WHERE fxKeyCustomer = @fxKeyCustomer AND fbCancelled = 0 UNION SELECT fxKeyJVNo as fxKey, 'tJVEntry' as fcTable, 'fxKeyJVNo' as fcCol, 'Journal Entry' as fcType, cast(right(left(fiEntryNo,6),4)+ right(left(fiEntryNo, 9),2)+ left(right(fiEntryNo, 8),2)+ right(fiEntryNo,5) as nVarchar) as fcNo, fdTransDate as fdDate, 'Journal Entries' as fcAccount, fdTotAmt as fdAmount FROM dbo.tJVEntry WHERE fxKeyNameID = @fxKeyCustomer UNION SELECT fxKeyDebit as fxKey, 'tDebitMemo' as fcTable, 'fxKeyDebit' as fcCol, 'Debit Memo' as fcType, fcDebitNo as fcNo, fdDateTrans as fdDate, '' as fcAccount, fdDBTol as fdAmount FROM dbo.tDebitMemo WHERE fxKeyName = @fxKeyCustomer UNION SELECT fxKeyPayment as fxKey, 'tPayment' as fcTable, 'fxKeyPayment' as fcCol, 'Payment' as fcType, fcRefNo as fcNo, fdDate, 'Undeposited Funds' as fcAccount, fdPayment as fdAmount FROM tPayment WHERE fxKeyCustomer = @fxKeyCustomer UNION SELECT fxKeySO as fxKey, 'tSalesOrder' as fcTable, 'fxKeySO' as fcCol, 'Sales Order' as fcType, fcSONo as fcNo , fdDateTransact as fdDate, 'Sales Orders' as fcAccount, fdTotal as fdAmount FROM tSalesOrder WHERE fxKeyCustomer = @fxKeyCustomer UNION SELECT fxKeySR as fxKey, 'tSalesReceipt' as fcTable, 'fxKeySR' as fcCol, 'Sales Receipt' as fcType, fcSaleNo as fcNo, fdDateTransact as fdDate, 'Undeposited Funds' as fcAccount, fdBalanceDue as fdAmount FROM tSalesReceipt WHERE fxKeyCustomer = @fxKeyCustomer )q WHERE fdDate BETWEEN @fdDateFrom AND @fdDateTo ORDER by fcType, fdDate END ENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO