Thanks for reply & sorry for late!
I'm using a proc with 2 parameters (whereClause & orderByClause) which pass to it from my app.
This proc generate & execute dynaimc query.
here is my proc t-sql code :
CREATE PROC [dbo].[sp_GetCash] @whereClause NVARCHAR(MAX), @orderBy NVARCHAR(MAX)
AS
DECLARE @query NVARCHAR(MAX)
SET @query=N'
select c.CashID, c.AcceptanceID, c.ZoneID, c.Hesab, c.SubmitDate, c.Description, c.CreatedBy, c.TimeCreated, c.DateCreated,
c.LastModifiedBy, c.LastModifiedTime, c.LastModifiedDate, c.ReceptionRowID, c.ReserveRowID, c.PaiedFromPackagePayment,
c.CashRowID, c.IsEbtal, c.DoctorID, r.ReceptionDate, r.TotalDiscount, c.OwnerTableName, c.OwnerID, c.PaymentTypeID,
c.HesabPrimary, c.HesabSecondary, c.POSMerchantID, c.POSTerminalID, c.POSRRN, c.POSStan, c.POSCardNumber, c.POSMessageID,
c.POSTransactionDate, c.POSTransactionTime, pt.PaymentTypeName, c.CashAcountID, c.DiffHesabAmount,
case when c.IsEbtal=1 then r.TotalPrice*-1 else r.TotalPrice end as ''TotalPrice'',
case
when c.PatientCaseID is null and r.PatientCaseID<>N''0'' then p.PatientFname +N'' ''+p.PatientLname
when c.PatientCaseID is null and r.PatientCaseID=N''0'' then r.PatientFirstName+N'' ''+r.PatientLastName
when c.PatientCaseID is not null then (select p2.PatientFname +N'' ''+p2.PatientLname from Patients p2 where p2.PatientCaseID=c.PatientCaseID)
when c.PatientCaseID is null and c.OwnerTableName=N''Reserve'' then (select r2.PatientFname +N'' ''+r2.PatientLname from Reserve r2 where r2.RowID=c.OwnerID)
end as ''PatientFullName'',
case
when c.PatientCaseID is null and c.OwnerTableName=N''Reserve'' then (select r2.PatientCaseID from Reserve r2 where r2.RowID=c.OwnerID)
when c.PatientCaseID is null and c.OwnerTableName<>N''Reserve'' then p.PatientCaseID else c.PatientCaseID
end as ''PatientCaseID'',
i1.InsuranceName as ''BasicInsuranceName'', i2.InsuranceName as ''SupplementaryInsuranceName''
from Cash c
JOIN Reception r ON c.OwnerID = r.ReceptionRowID and c.OwnerTableName=N''Reception''
LEFT JOIN Doctors d ON c.DoctorID = d.DoctorID
LEFT JOIN Patients p ON r.PatientCaseID = p.PatientCaseID
LEFT JOIN Insurances i1 on r.BasicInsuranceID=i1.InsuranceID
LEFT JOIN Insurances i2 on r.SupplementaryInsuranceID=i2.InsuranceID
JOIN PaymentsTypes pt on c.PaymentTypeID=pt.PaymentTypeID
union all
select c.CashID, c.AcceptanceID, c.ZoneID, c.Hesab, c.SubmitDate, c.Description, c.CreatedBy, c.TimeCreated, c.DateCreated,
c.LastModifiedBy, c.LastModifiedTime, c.LastModifiedDate, c.ReceptionRowID, c.ReserveRowID, c.PaiedFromPackagePayment,
c.CashRowID, c.IsEbtal, c.DoctorID, ''-'', 0, c.OwnerTableName, c.OwnerID, c.PaymentTypeID, c.HesabPrimary, c.HesabSecondary, c.POSMerchantID,
c.POSTerminalID, c.POSRRN, c.POSStan, c.POSCardNumber, c.POSMessageID, c.POSTransactionDate, c.POSTransactionTime,
pt.PaymentTypeName, c.CashAcountID, c.DiffHesabAmount, 0 as ''TotalPrice'', p.PatientFname +N'' ''+p.PatientLname as ''PatientFullName'', c.PatientCaseID,
N''-'' as ''BasicInsuranceName'', N''-'' as ''SupplementaryInsuranceName''
from Cash c
LEFT JOIN Doctors d ON c.DoctorID = d.DoctorID
JOIN Patients p ON c.PatientCaseID = p.PatientCaseID
JOIN PaymentsTypes pt on c.PaymentTypeID=pt.PaymentTypeID
where c.PatientCaseID is not null and c.OwnerTableName is null and c.OwnerID is null and c.PaiedFromPackagePayment=1
union all
select c.CashID, c.AcceptanceID, c.ZoneID, c.Hesab, c.SubmitDate, c.Description, c.CreatedBy, c.TimeCreated, c.DateCreated,
c.LastModifiedBy, c.LastModifiedTime, c.LastModifiedDate, c.ReceptionRowID, c.ReserveRowID, c.PaiedFromPackagePayment,
c.CashRowID, c.IsEbtal, c.DoctorID, ''-'', 0, c.OwnerTableName, c.OwnerID, c.PaymentTypeID, c.HesabPrimary, c.HesabSecondary, c.POSMerchantID,
c.POSTerminalID, c.POSRRN, c.POSStan, c.POSCardNumber, c.POSMessageID, c.POSTransactionDate, c.POSTransactionTime, pt.PaymentTypeName, c.CashAcountID, c.DiffHesabAmount, 0 as ''TotalPrice'',
res.PatientFname +N'' ''+res.PatientLname as ''PatientFullName'', res.PatientCaseID, N''-'' as ''BasicInsuranceName'', N''-'' as ''SupplementaryInsuranceName''
from Cash c
JOIN Reserve res ON c.OwnerID = res.RowID and c.OwnerTableName=N''Reserve''
LEFT JOIN Doctors d ON c.DoctorID = d.DoctorID
JOIN PaymentsTypes pt on c.PaymentTypeID=pt.PaymentTypeID
'
if(dbo.fn_IsLISActive()=1)
BEGIN
SET @query+=N'
union all
select c.CashID, c.AcceptanceID, c.ZoneID, c.Hesab, c.SubmitDate, c.Description, c.CreatedBy, c.TimeCreated, c.DateCreated,
c.LastModifiedBy, c.LastModifiedTime, c.LastModifiedDate, c.ReceptionRowID, c.ReserveRowID, c.PaiedFromPackagePayment,
c.CashRowID, c.IsEbtal, c.DoctorID, r.ReceptionDate, r.TotalDiscount, c.OwnerTableName, c.OwnerID, c.PaymentTypeID,
c.HesabPrimary, c.HesabSecondary, c.POSMerchantID, c.POSTerminalID, c.POSRRN, c.POSStan, c.POSCardNumber,
c.POSMessageID, c.POSTransactionDate, c.POSTransactionTime, pt.PaymentTypeName, c.CashAcountID, c.DiffHesabAmount,
case when c.IsEbtal=1 then r.TotalPrice*-1 else r.TotalPrice end as ''TotalPrice'',
p.PatientFname +N'' ''+p.PatientLname as ''PatientFullName'', p.PatientCaseID,
i1.InsuranceName as ''BasicInsuranceName'', i2.InsuranceName as ''SupplementaryInsuranceName''
from Cash c
JOIN Lab_Receptions r ON c.OwnerID = r.ReceptionRowID and c.OwnerTableName=N''Lab_Receptions''
LEFT JOIN Doctors d ON c.DoctorID = d.DoctorID
LEFT JOIN Patients p ON r.PatientCaseID = p.PatientCaseID
LEFT JOIN Insurances i1 on r.BasicInsuranceID=i1.InsuranceID
LEFT JOIN Insurances i2 on r.SupplementaryInsuranceID=i2.InsuranceID
JOIN PaymentsTypes pt on c.PaymentTypeID=pt.PaymentTypeID
'
END
if(dbo.fn_IsDentalActive()=1)
BEGIN
SET @query+=N'
union all
select c.CashID, c.AcceptanceID, c.ZoneID, c.Hesab, c.SubmitDate, c.Description, c.CreatedBy, c.TimeCreated, c.DateCreated,
c.LastModifiedBy, c.LastModifiedTime, c.LastModifiedDate, c.ReceptionRowID, c.ReserveRowID, c.PaiedFromPackagePayment,
c.CashRowID, c.IsEbtal, c.DoctorID, dr.ReceptionDate, dr.TotalDiscount, c.OwnerTableName, c.OwnerID, c.PaymentTypeID,
c.HesabPrimary, c.HesabSecondary, c.POSMerchantID, c.POSTerminalID, c.POSRRN, c.POSStan, c.POSCardNumber, c.POSMessageID,
c.POSTransactionDate, c.POSTransactionTime, pt.PaymentTypeName, c.CashAcountID, c.DiffHesabAmount,
case when c.IsEbtal=1 then dr.TotalPrice*-1 else dr.TotalPrice end as ''TotalPrice'',
p.PatientFname +N'' ''+p.PatientLname as ''PatientFullName'', p.PatientCaseID,
i1.InsuranceName as ''BasicInsuranceName'', i2.InsuranceName as ''SupplementaryInsuranceName''
from Cash c
JOIN Dental_Receptions dr ON c.OwnerID = dr.ReceptionRowID and c.OwnerTableName=N''Dental_Receptions''
LEFT JOIN Doctors d ON c.DoctorID = d.DoctorID
LEFT JOIN Patients p ON dr.PatientCaseID = p.PatientCaseID
LEFT JOIN Insurances i1 on dr.BasicInsuranceID=i1.InsuranceID
LEFT JOIN Insurances i2 on dr.SupplementaryInsuranceID=i2.InsuranceID
JOIN PaymentsTypes pt on c.PaymentTypeID=pt.PaymentTypeID
'
END
if(dbo.fn_IsRISActive()=1)
BEGIN
SET @query+=N'
union all
select c.CashID, c.AcceptanceID, c.ZoneID, c.Hesab, c.SubmitDate, c.Description, c.CreatedBy, c.TimeCreated, c.DateCreated,
c.LastModifiedBy, c.LastModifiedTime, c.LastModifiedDate, c.ReceptionRowID, c.ReserveRowID, c.PaiedFromPackagePayment,
c.CashRowID, c.IsEbtal, c.DoctorID, r.ReceptionDate, r.TotalDiscounts, c.OwnerTableName, c.OwnerID, c.PaymentTypeID,
c.HesabPrimary, c.HesabSecondary, c.POSMerchantID, c.POSTerminalID, c.POSRRN, c.POSStan, c.POSCardNumber,
c.POSMessageID, c.POSTransactionDate, c.POSTransactionTime, pt.PaymentTypeName, c.CashAcountID, c.DiffHesabAmount,
case when c.IsEbtal=1 then r.TotalPrice*-1 else r.TotalPrice end as ''TotalPrice'',
p.PatientFname +N'' ''+p.PatientLname as ''PatientFullName'', p.PatientCaseID,
i1.InsuranceName as ''BasicInsuranceName'', i2.InsuranceName as ''SupplementaryInsuranceName''
from Cash c
JOIN Imaging_Receptions r ON c.OwnerID = r.ReceptionRowID and c.OwnerTableName=N''Imaging_Receptions''
LEFT JOIN Doctors d ON c.DoctorID = d.DoctorID
LEFT JOIN Patients p ON r.PatientCaseID = p.PatientCaseID
LEFT JOIN Insurances i1 on r.BasicInsuranceID=i1.InsuranceID
LEFT JOIN Insurances i2 on r.SupInsuranceID=i2.InsuranceID
JOIN PaymentsTypes pt on c.PaymentTypeID=pt.PaymentTypeID
'
END
SET @query=N'with cte as ('+@query+N')
SELECT c.* FROM cte c'
IF(len(@whereClause)>0)
set @query=@query+N' where '+@whereClause
if(len(@orderBy)>0)
set @query=@query+N' order by '+@orderBy
exec sp_executesql @query
The code which works is :
EXEC sp_GetCash @whereClause=N'',@orderBy=N'c.TimeCreated desc'
And the code which take a long time to complete is :
EXEC sp_GetCash N'c.ReceptionDate >= ''1399/10/01'' and c.ReceptionDate <= ''1399/10/20'' ', N'c.TimeCreated desc'