Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

How to append where clause to existing statement without dynamic SQL ?

$
0
0

Hi,

I have SQL statement like this ..

CREATE PROCEDURE [dbo].[GetProspectBySearch]
 @StartRowIndex AS INT,
 @MaximumRows AS INT,
 @ModuleType AS VARCHAR(50) = NULL,
 @ProspectCategory AS VARCHAR(50) = NULL,
 @ProspectsWithoutContact AS BIT = NULL
AS
BEGIN
	IF @Status = 'All'
	BEGIN
		SET @Status = NULL
	END-- Total count records with where conditions ( not paging ) 
	IF @ProspectsWithoutContact = '1'
	BEGIN
		SELECT COUNT(*)
		FROM Prospect
		WHERE ProspectsCode IS NOT NULLAND ContactPerson = NULL AND TelNo = NULL AND FaxNo = NULL
		AND [ProspectsCode] <> ''
		AND (ModuleType LIKE '%' + @ModuleType + '%' OR @ModuleType = '' OR @ModuleType IS NULL)
		AND (ProspectCategory LIKE '%' + @ProspectCategory + '%' OR @ProspectCategory = '' OR @ProspectCategory IS NULL)
	END
	ELSE
	BEGIN
		SELECT COUNT(*)
		FROM Prospect
		WHERE ProspectsCode IS NOT NULL AND [ProspectsCode] <> ''
		AND (ModuleType LIKE '%' + @ModuleType + '%' OR @ModuleType = '' OR @ModuleType IS NULL)
		AND (ProspectCategory LIKE '%' + @ProspectCategory + '%' OR @ProspectCategory = '' OR @ProspectCategory IS NULL)

	END

	--Get records with where conditions ( paging)
	IF @ProspectsWithoutContact = '1'
	BEGIN
		SELECT *
		FROM (
			SELECT ROW_NUMBER() OVER (
					ORDER BY NAME DESC
					) AS RowRank, [ProspectId], [Z_ID], [ProspectsCode], [ModuleType], [CustomerCode], [AccountCode], [CustomerType], [Name], [Name2], [ProspectCategory], [Status], [Initial], [ShortName], [CreateClientNoParent], [Industry], [FinancialYearEnd], [GroupCompany], [NoOfEmployees], [ParentCompany], [CurrrencyCode], [Incorporated], [Registered], [YearEstablished], [IsIntroducer], [JMBCertNum], [Occupation], [ClientBanding], [RiskExposure], [HoldingHongLeongBank], [TypeOfIndustry], [ProductsAndServices], [Category], [Unit], [PrevUnit], [MarketingExec], [PrevMarketingExec], [ServicingExec], [CoServicer], [Introducer], [ExternalServicer], [OffStreetAddress], [OffCountry], [OffTown], [OffPostCode], [OffState], [SameAsOffAddress], [PosStreetAddress], [PosCountry], [PosTown], [PosPostCode], [PosState], [ContactPerson], [Email], [TelNo], [FaxNo], [Circular], [FestivalCards], [ProductBrochure], [Statements], [PremiumWarranty], [RenewalNotice], [FreeTradeZone], [GlobalBroker], [CreatedBy], [CreatedDate], [ModifiedBy], [ModifiedDate], [DateStatus],
				[DateImported]
			FROM Prospect
			WHERE ProspectsCode IS NOT NULLAND ContactPerson = NULL AND TelNo = NULL AND FaxNo = NULL 
			AND [ProspectsCode] <> ''
			AND (ModuleType LIKE '%' + @ModuleType + '%' OR @ModuleType = '' OR @ModuleType IS NULL)
			AND (ProspectCategory LIKE '%' + @ProspectCategory + '%' OR @ProspectCategory = '' OR @ProspectCategory IS NULL)
			) AS ProspectWithRowNumbers
		WHERE RowRank > @startRowIndex AND RowRank <= (@startRowIndex + @maximumRows)
	END
	ELSE
	BEGIN
		SELECT *
		FROM (
			SELECT ROW_NUMBER() OVER (
					ORDER BY NAME DESC
					) AS RowRank, [ProspectId], [Z_ID], [ProspectsCode], [ModuleType], [CustomerCode], [AccountCode], [CustomerType], [Name], [Name2], [ProspectCategory], [Status], [Initial], [ShortName], [CreateClientNoParent], [Industry], [FinancialYearEnd], [GroupCompany], [NoOfEmployees], [ParentCompany], [CurrrencyCode], [Incorporated], [Registered], [YearEstablished], [IsIntroducer], [JMBCertNum], [Occupation], [ClientBanding], [RiskExposure], [HoldingHongLeongBank], [TypeOfIndustry], [ProductsAndServices], [Category], [Unit], [PrevUnit], [MarketingExec], [PrevMarketingExec], [ServicingExec], [CoServicer], [Introducer], [ExternalServicer], [OffStreetAddress], [OffCountry], [OffTown], [OffPostCode], [OffState], [SameAsOffAddress], [PosStreetAddress], [PosCountry], [PosTown], [PosPostCode], [PosState], [ContactPerson], [Email], [TelNo], [FaxNo], [Circular], [FestivalCards], [ProductBrochure], [Statements], [PremiumWarranty], [RenewalNotice], [FreeTradeZone], [GlobalBroker], [CreatedBy], [CreatedDate], [ModifiedBy], [ModifiedDate], [DateStatus],
				[DateImported]
			FROM Prospect
			WHERE ProspectsCode IS NOT NULL
			AND [ProspectsCode] <> ''
			AND (ModuleType LIKE '%' + @ModuleType + '%' OR @ModuleType = '' OR @ModuleType IS NULL)
			AND (ProspectCategory LIKE '%' + @ProspectCategory + '%' OR @ProspectCategory = '' OR @ProspectCategory IS NULL)
			) AS ProspectWithRowNumbers
		WHERE RowRank > @startRowIndex AND RowRank <= (@startRowIndex + @maximumRows)
	END

END

1) How do I can write count and paging statements in to single statement ?

2) There is parameter @ProspectsWithoutContact is using for differentiation  for count and paging statements. If @ProspectsWithoutContact is "1" then this clause ( AND ContactPerson = NULL AND TelNo = NULL AND FaxNo = NULL) will be added in to both count and paging statements. How do I can write this in to single statement with out IF ELSE ?

Please help to write normal SQL with out dynamic SQL ?

Thank you.


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>