Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi members,

I have a set of SQL statement which I will need to make use of the where condition but I would like to know how to select all with the where condition in place. Thus what will be required to be inserted in the parameters to make it select all?
SQL
@param1 nvarchar(max),
@param2 nvarchar(max)

DECLARE @SQL nvarchar(max)


SET @SQL= 'SELECT *
FROM Table1
WHERE Column1 IN (@param1) AND Column2 IN (@param2)'

EXEC sp_executesql @SQL; 


I tried putting the column name in to the param e.g column1 into @param1 but it does not return me all the data. For example table1 consist of 4524 rows but using the above it only returns me 309 rows. How can I solve this?
Posted
Updated 6-Oct-15 19:40pm
v3
Comments
Maciej Los 7-Oct-15 2:37am    
SELECT * FROM Table1 returns all data.

SQL
WHERE 1 = 1


But why not just get rid of the WHERE clause totally instead?
 
Share this answer
 
Comments
waynetan123 7-Oct-15 1:28am    
The reason being is actually Im passing in parameters thus sometimes it can be based on condition and sometimes it can be select all. How do I use the where 1 = 1? I need the in clause actually
waynetan123 7-Oct-15 1:41am    
Please refer to my updated questions thanks
Maciej Los 7-Oct-15 2:35am    
+5!
Hi,

Check this...

SQL
SELECT *
FROM Table1
WHERE Column1 = CASE WHEN @para_Col1 <> '' then @para_Col1 ELSE Column1 End
AND Column2 = CASE WHEN @para_Col2 <> '' then @para_Col2 ELSE Column2 End
AND Column3 = CASE WHEN @para_Col3 <> '' then @para_Col3 ELSE Column3 End
AND Column4 = CASE WHEN @para_Col4 <> '' then @para_Col4 ELSE Column4 End
--Note here i am assuming only four columns you have in your table1.


Update one...
SQL
@param1 nvarchar(max),
@param2 nvarchar(max)
 
SELECT *
FROM Table1
WHERE Column1 = CASE WHEN @param1 <> '' Then @param1 Else Column1 end
AND Column2 = CASE WHEN @param2 <> '' Then @param2 Else Column2 end

--i think there is no need of dynamic query.


As per your conditions, you should develop a logic to identify whether "Select All" has been checked or not. On that basis you can customize your query as below.

SQL
@param1 nvarchar(max),--pass 1 when Select All is checked
@param2 nvarchar(max),--pass 1 when Select All is checked

 
SELECT *
FROM Table1
WHERE Column1 IN CASE WHEN @param1 = '1' then Cloumn1 else @param1 end
AND Column2 IN CASE WHEN @param2 = '1' then Cloumn2 else @param2 end


--if You can do this Hard coding in your code then
SELECT *
FROM Table1
WHERE Column1 IN CASE WHEN @param1 = 'Cloumn1' then Cloumn1 else @param1 end
AND Column2 IN CASE WHEN @param2 = 'Cloumn2' then Cloumn2 else @param2 end



Hope this will help you.

Cheers
 
Share this answer
 
v4
Comments
waynetan123 7-Oct-15 1:36am    
Hi I have more than 4 columns but only 4 columns requires the where condition
Magic Wonder 7-Oct-15 1:43am    
You can use CASE in where clause, it will work and complete your requirements.
Magic Wonder 7-Oct-15 1:44am    
Try my solution.
waynetan123 7-Oct-15 1:49am    
If I have lets say where Column1 = Column1 and Column2 = Column2 it will return me lesser rows as compared to the actual table. I tried as explain in my question thanks
Magic Wonder 7-Oct-15 1:53am    
You mean to say, irrespective of where clause/parameter you need all rows???
Hi As pert your comments
'lets say in column1 there are 4 names but user can select maybe tom and mary which It will pass into the parameter like this 'tom,'mary' however if user click on select all it will pass into the param 'column1' which are my current code logic'

change the logic like
Pass the 'select all' as one of parameter to your SP/Query,based on this parameter write the query in your logic

ex:
SQL
declare @Selectall Bit
@param1 nvarchar(max),
@param2 nvarchar(max)
if @Selectall > 0
begin
   SELECT * FROM Table1 where 1=1
end
else
begin
   SELECT * FROM Table1 where column1 in (@param1)
end


take this as refernce try with your requirement...
 
Share this answer
 
Comments
waynetan123 7-Oct-15 2:39am    
I get what you are trying to do but I do have more than 1 parameters to be passed so I don't think this can work.
vangapally Naveen Kumar 7-Oct-15 3:26am    
If you are using select all for every column??
if it the case put an flag based on the flag do the code changes,lets say column for select all and column2 for also selectall then set the flag to 1 else 0

morover you can pass the 'n' number of parameters in where clause.

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