can any one help me to refactor this store proc
it seems to me very ugly
ALTER PROCEDURE [dbo].[SearchTransactions]
@SiteId varchar(4)
, @ProviderId int
, @PatientFirstName varchar(35)
, @PatientLastName varchar(60)
, @UnsentTransactionsOnly bit
, @GEDIPayerId varchar(10)
, @SubscriberId varchar(20)
, @TemplateId int
, @TypeOfDateSearch varchar(20)
, @DateSearchFrom datetime
, @DateSearchTo datetime
, @Debug bit = 0
AS
BEGIN
DECLARE @SQL nvarchar(max)
, @ParamList nvarchar(4000);
--Adds day to properly handle date range logic (ex. 8/12/2011 would become 8/13/2011 for < @DateSearchTo filter)
SET @DateSearchTo = DATEADD(day, 1, @DateSearchTo);
IF @UnsentTransactionsOnly = 1
SET @SQL = 'SELECT q.RTPARequestId
, q.PatientFirstName
, q.PatientLastName
, v.FirstName + '' ''+ v.LastName AS Provider
, q.SubmissionDate
, q.DateOfServiceFrom
, p.PayerName
, t.TemplateName
, q.RTPARequest
, case when q.ProviderId=2 then 1 else 0 end as ProviderIsValid
FROM dbo.RTPARequest q
INNER JOIN dbo.Payer p ON q.GEDIPayerId = p.GEDIPayerId
LEFT JOIN dbo.Provider v ON q.ProviderId = v.ProviderId
LEFT JOIN dbo.RTPAResponse s ON q.RTPARequestId = s.RTPARequestId
LEFT JOIN dbo.Template t ON q.TemplateId = t.TemplateId
WHERE q.SiteId = @SiteId' + CHAR(13) + CHAR(10) ;
ELSE
SET @SQL = 'SELECT q.RTPARequestId
, q.PatientFirstName
, q.PatientLastName
, v.FirstName + '' ''+ v.LastName AS Provider
, q.SubmissionDate
, q.DateOfServiceFrom
, p.PayerName
, t.TemplateName
, q.RTPARequest
, s.RTPAResponse
, case when q.ProviderId=2 then 1 else 0 end as ProviderIsValid
FROM dbo.RTPARequest q
INNER JOIN dbo.Payer p ON q.GEDIPayerId = p.GEDIPayerId
LEFT JOIN dbo.Provider v ON q.ProviderId = v.ProviderId
LEFT JOIN dbo.RTPAResponse s ON q.RTPARequestId = s.RTPARequestId
LEFT JOIN dbo.Template t ON q.TemplateId = t.TemplateId
WHERE q.SiteId = @SiteId' + CHAR(13) + CHAR(10) ;
IF @ProviderId IS NOT NULL
SET @SQL = @SQL + ' AND q.ProviderId = @ProviderId' + CHAR(13) + CHAR(10);
IF @PatientFirstName IS NOT NULL
SET @SQL = @SQL + ' AND q.PatientFirstName = @PatientFirstName' + CHAR(13) + CHAR(10);
IF @PatientLastName IS NOT NULL
SET @SQL = @SQL + ' AND q.PatientLastName = @PatientLastName' + CHAR(13) + CHAR(10);
IF @GEDIPayerId IS NOT NULL
SET @SQL = @SQL + ' AND q.GEDIPayerId = @GEDIPayerId' + CHAR(13) + CHAR(10);
IF @SubscriberId IS NOT NULL
SET @SQL = @SQL + ' AND q.SubscriberId = @SubscriberId' + CHAR(13) + CHAR(10);
IF @TemplateId IS NOT NULL
SET @SQL = @SQL + ' AND q.TemplateId = @TemplateId' + CHAR(13) + CHAR(10);
IF @TypeOfDateSearch = 'Date Of Service'
BEGIN
IF @DateSearchFrom IS NOT NULL
SET @SQL = @SQL + ' AND q.DateOfServiceFrom >= @DateSearchFrom' + CHAR(13) + CHAR(10);
IF @DateSearchTo IS NOT NULL
SET @SQL = @SQL + ' AND q.DateOfServiceFrom < @DateSearchTo' + CHAR(13) + CHAR(10);
END
ELSE
IF @TypeOfDateSearch = 'Date Of Submission'
BEGIN
IF @DateSearchFrom IS NOT NULL
SET @SQL = @SQL + ' AND q.SubmissionDate >= @DateSearchFrom' + CHAR(13) + CHAR(10);
IF @DateSearchTo IS NOT NULL
SET @SQL = @SQL + ' AND q.SubmissionDate < @DateSearchTo' + CHAR(13) + CHAR(10);
END
IF @UnsentTransactionsOnly = 1
SET @SQL = @SQL + ' AND s.RTPARequestId IS NULL' + CHAR(13) + CHAR(10);
IF @Debug = 1
PRINT @SQL;
SET @ParamList = '@SiteId varchar(4),
@ProviderId int,
@PatientFirstName varchar(35),
@PatientLastName varchar(60),
@GEDIPayerId varchar(10),
@SubscriberId varchar(20),
@TemplateId int,
@TypeOfDateSearch varchar(20),
@DateSearchFrom datetime,
@DateSearchTo datetime';
EXEC sp_executesql @SQL, @ParamList
, @SiteId
, @ProviderId
, @PatientFirstName
, @PatientLastName
, @GEDIPayerId
, @SubscriberId
, @TemplateId
, @TypeOfDateSearch
, @DateSearchFrom
, @DateSearchTo;
RETURN @@ERROR ;
END
thanks
it seems to me very ugly
ALTER PROCEDURE [dbo].[SearchTransactions]
@SiteId varchar(4)
, @ProviderId int
, @PatientFirstName varchar(35)
, @PatientLastName varchar(60)
, @UnsentTransactionsOnly bit
, @GEDIPayerId varchar(10)
, @SubscriberId varchar(20)
, @TemplateId int
, @TypeOfDateSearch varchar(20)
, @DateSearchFrom datetime
, @DateSearchTo datetime
, @Debug bit = 0
AS
BEGIN
DECLARE @SQL nvarchar(max)
, @ParamList nvarchar(4000);
--Adds day to properly handle date range logic (ex. 8/12/2011 would become 8/13/2011 for < @DateSearchTo filter)
SET @DateSearchTo = DATEADD(day, 1, @DateSearchTo);
IF @UnsentTransactionsOnly = 1
SET @SQL = 'SELECT q.RTPARequestId
, q.PatientFirstName
, q.PatientLastName
, v.FirstName + '' ''+ v.LastName AS Provider
, q.SubmissionDate
, q.DateOfServiceFrom
, p.PayerName
, t.TemplateName
, q.RTPARequest
, case when q.ProviderId=2 then 1 else 0 end as ProviderIsValid
FROM dbo.RTPARequest q
INNER JOIN dbo.Payer p ON q.GEDIPayerId = p.GEDIPayerId
LEFT JOIN dbo.Provider v ON q.ProviderId = v.ProviderId
LEFT JOIN dbo.RTPAResponse s ON q.RTPARequestId = s.RTPARequestId
LEFT JOIN dbo.Template t ON q.TemplateId = t.TemplateId
WHERE q.SiteId = @SiteId' + CHAR(13) + CHAR(10) ;
ELSE
SET @SQL = 'SELECT q.RTPARequestId
, q.PatientFirstName
, q.PatientLastName
, v.FirstName + '' ''+ v.LastName AS Provider
, q.SubmissionDate
, q.DateOfServiceFrom
, p.PayerName
, t.TemplateName
, q.RTPARequest
, s.RTPAResponse
, case when q.ProviderId=2 then 1 else 0 end as ProviderIsValid
FROM dbo.RTPARequest q
INNER JOIN dbo.Payer p ON q.GEDIPayerId = p.GEDIPayerId
LEFT JOIN dbo.Provider v ON q.ProviderId = v.ProviderId
LEFT JOIN dbo.RTPAResponse s ON q.RTPARequestId = s.RTPARequestId
LEFT JOIN dbo.Template t ON q.TemplateId = t.TemplateId
WHERE q.SiteId = @SiteId' + CHAR(13) + CHAR(10) ;
IF @ProviderId IS NOT NULL
SET @SQL = @SQL + ' AND q.ProviderId = @ProviderId' + CHAR(13) + CHAR(10);
IF @PatientFirstName IS NOT NULL
SET @SQL = @SQL + ' AND q.PatientFirstName = @PatientFirstName' + CHAR(13) + CHAR(10);
IF @PatientLastName IS NOT NULL
SET @SQL = @SQL + ' AND q.PatientLastName = @PatientLastName' + CHAR(13) + CHAR(10);
IF @GEDIPayerId IS NOT NULL
SET @SQL = @SQL + ' AND q.GEDIPayerId = @GEDIPayerId' + CHAR(13) + CHAR(10);
IF @SubscriberId IS NOT NULL
SET @SQL = @SQL + ' AND q.SubscriberId = @SubscriberId' + CHAR(13) + CHAR(10);
IF @TemplateId IS NOT NULL
SET @SQL = @SQL + ' AND q.TemplateId = @TemplateId' + CHAR(13) + CHAR(10);
IF @TypeOfDateSearch = 'Date Of Service'
BEGIN
IF @DateSearchFrom IS NOT NULL
SET @SQL = @SQL + ' AND q.DateOfServiceFrom >= @DateSearchFrom' + CHAR(13) + CHAR(10);
IF @DateSearchTo IS NOT NULL
SET @SQL = @SQL + ' AND q.DateOfServiceFrom < @DateSearchTo' + CHAR(13) + CHAR(10);
END
ELSE
IF @TypeOfDateSearch = 'Date Of Submission'
BEGIN
IF @DateSearchFrom IS NOT NULL
SET @SQL = @SQL + ' AND q.SubmissionDate >= @DateSearchFrom' + CHAR(13) + CHAR(10);
IF @DateSearchTo IS NOT NULL
SET @SQL = @SQL + ' AND q.SubmissionDate < @DateSearchTo' + CHAR(13) + CHAR(10);
END
IF @UnsentTransactionsOnly = 1
SET @SQL = @SQL + ' AND s.RTPARequestId IS NULL' + CHAR(13) + CHAR(10);
IF @Debug = 1
PRINT @SQL;
SET @ParamList = '@SiteId varchar(4),
@ProviderId int,
@PatientFirstName varchar(35),
@PatientLastName varchar(60),
@GEDIPayerId varchar(10),
@SubscriberId varchar(20),
@TemplateId int,
@TypeOfDateSearch varchar(20),
@DateSearchFrom datetime,
@DateSearchTo datetime';
EXEC sp_executesql @SQL, @ParamList
, @SiteId
, @ProviderId
, @PatientFirstName
, @PatientLastName
, @GEDIPayerId
, @SubscriberId
, @TemplateId
, @TypeOfDateSearch
, @DateSearchFrom
, @DateSearchTo;
RETURN @@ERROR ;
END
thanks