I actually wonder whether you need to do this with Dynamic SQL. There is a method I've been using a lot lately that seems fast and doesn't involve the (rather fragile) risky business of dynamic sql.
Let's say you have a webpage that has a search form:
Travel Center {drop down with ID's - with the Select One choice set to a value of zero}
Region {drop down with ID's - again, the top choice value is zero}
Select Count(TravelOffersID)
FROM
{your joined together table structure}
WHERE
(@TravelCenterID = '0' OR [dbo].[TravelOffers].[Trav
and (@RegionID = '0' OR [dbo].[TravelOffers].[Regi
and....
and
( [dbo].[TravelOffers].[Trav
OR [dbo].[TravelOffers].[Regi
OR.....
)
This method doesn't require any If/ Logic or dynamic SQL. It is very fast and reliable. You will be amazed at how powerful this WHERE scheme can be. Basically, you can set up a bare minimum of procedures to cover all sorts of different select requirements.
Main Topics
Browse All Topics





by: NightmanPosted on 2008-10-16 at 23:46:16ID: 22738215
You don't need the extra variable declaration
Change this line:
@MyItemCount = ISNULL(COUNT(*),0)
to
@ItemCount = ISNULL(COUNT(*),0)
and this line
EXEC sp_executesql @MySql, N'@MyItemCount bigint OUTPUT', @ItemCount = @MyItemCount OUTPUT
To
EXEC sp_executesql @MySql, N'@ItemCount bigint OUTPUT', @ItemCount OUTPUT