February 3, 2014 at 5:02 am
Dear,
I require to write a single query that if I pass a customer id, it will retrieve that customer's information. If I do not pass any customer id, it will retrieve all customers' information.
Please help me to provide any idea.
Regards,
Akbar
February 3, 2014 at 5:40 am
Please post ddl and dml for the tables involved in your query.
This is quite a trivial request and there are several ways to do it - what have you tried?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 3, 2014 at 8:23 am
read up at Gails blog on catch all queries: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
here's an example of a catch all query:
WHERE 1 = CASE
WHEN @CustomerID IS NULL
THEN 1
WHEN CustomerID = @CustomerID
THEN 1
ELSE 0
END
Lowell
February 3, 2014 at 8:37 am
That kind of query generally doesn't perform well. To give you an idea, I had a query the other week that was written to do just that and took 3 hours to run. When I removed the portion of the query that handled 'one customer or all customers', the query ran in under 2 seconds.
Much better to have the app decide which query to run or, if that's not possible, for the stored procedure to decide whether to run a procedure to return one customer or a procedure to return all customers.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 3, 2014 at 12:06 pm
To add to that, unless you only have a 100 customers or so, I think it a bit insane to return all customers at any time.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 3, 2014 at 10:40 pm
Thanks all. Specially to GilaMonster and Lowell for your valuable suggestions.
It helped me.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply