Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing this query for report viewer :

SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
WHERE (Clients_Title = @Clients_Title) OR
(Address_Current = @Address_Current) OR
(Phone_Number = @Phone_Number) OR
(Mobile_Number = @Mobile_Number) OR
(AreaLocation = @AreaLocation)
What I want is:

When there are no parameter that is provided to the report viewer, all of the customer record will be displayed.
If there are parameters provided, the OR condition is implemented .
Lastly, if there is contrast in any where condition, then no record will be displayed.

Can anyone tell me how I can accomplish this?
Posted

try this
SQL
if @Clients_Title != NULL OR @Address_Current != NULL OR @Phone_Number != NULL OR @Mobile_Number !+ NULL OR @AreaLocation != NULL 
Begin 
SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
WHERE (Clients_Title = @Clients_Title) OR
(Address_Current = @Address_Current) OR
(Phone_Number = @Phone_Number) OR
(Mobile_Number = @Mobile_Number) OR
(AreaLocation = @AreaLocation)
else
SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
End
 
Share this answer
 
Comments
Member 8840306 12-Nov-13 6:58am    
this cant be written in report viewer ...is it store procedure in vb? ..I am using C# .. I don't know the usage of store procedure with crystal report . So kindly Tell me query ..
prashant patil 4987 12-Nov-13 7:05am    
u make your stored procudure like above solution..
answer ill appear as you expected..ok..
SQL
SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
WHERE (Clients_Title = ISNULL(@Clients_Title,Clients_Title)) OR
(Address_Current = ISNULL(@Address_Current,Address_Current)) OR
(Phone_Number = ISNULL(@Phone_Number,Phone_Number)) OR
(Mobile_Number = ISNULL(@Mobile_Number,Mobile_Number)) OR
(AreaLocation = ISNULL(@AreaLocation,AreaLocation))
 
Share this answer
 
This can be executed as a query, but stored procedure is best for better performance.
SQL
SELECT
    Cust_Id, Clients_Title, Card_Number, Key_Person,
    Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
WHERE
    (@Clients_Title IS NULL OR Clients_Title = @Clients_Title) AND
    (@Address_Current IS NULL OR Address_Current = @Address_Current) AND
    (@Phone_Number IS NULL OR Phone_Number = @Phone_Number) AND
    (@Mobile_Number IS NULL OR Mobile_Number = @Mobile_Number) AND
    (@AreaLocation IS NULL OR AreaLocation = @AreaLocation)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900