Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help

Author  Topic 

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-28 : 00:48:25
ALTER procedure [search_fields]
(
@drpSelectValue VARCHAR(30) = null, -- this is column name of employees table
@txtValue as varchar(10)=null -- value to be searched in corresponding column data
)
As
begin

if @drpSelectValue = 'last_name'
begin

select *from employees where
@drpSelectValue like (case when @txtValue is not null then @txtValue+'%' else @drpSelectValue end)
end
else
Begin
select * from employees where
@drpSelectValue=(case when @txtValue is not null then @txtValue else @drpSelectValue end)
End
End

How to make that red part as a column type

--
Chandu

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-11-28 : 01:11:29
Dynamic SQL is the only way
http://www.sommarskog.se/dynamic_sql.html


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-28 : 01:24:09
Thank you Khtan... I rectified this problem with dynamic query...

ALTER procedure [search_fields]
(
@drpSelectValue VARCHAR(50) = null,
@txtValue as varchar(10)=null
)
As
begin
DECLARE @SQL varchar(max) =''
if @drpSelectValue = 'username'
begin
SET @SQL = 'select * from Userdetails where ' + @drpSelectValue +' like (case when '''+@txtValue +''' is not null then '''+ @txtValue+ '%'' else ' + @drpSelectValue +' end)'
end
else
Begin
SET @SQL = 'select * from Userdetails where ' + @drpSelectValue +' = (case when '''+@txtValue +''' is not null then '''+ @txtValue+ ''' else ' + @drpSelectValue +' end)'
End
EXEC (@SQL)
End
GO

exec search_fields 'username', 'king'
exec search_fields 'username', null -- Problem

There is one more problem when i pass NULL to @txtValue..



--
Chandu
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-11-28 : 01:53:23
what do you expect the result to be when you pass in NULL value to the @txtValue ?

you have to handle that logic when you generate the dynamic sql statement


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-28 : 02:23:48
quote:
Originally posted by khtan

what do you expect the result to be when you pass in NULL value to the @txtValue ?

you have to handle that logic when you generate the dynamic sql statement


Ya i knew that. If @txtValue is null then all records sholud be in output..

For that i changed my query as follows:

if @drpSelectValue = 'last_name' AND @txtValue IS NOT NULL
begin
SET @SQL = 'select * from employees where ' + @drpSelectValue +' like '''+ @txtValue + '%'' '
end
ELSE
SET @SQL = 'select * from employees'


But am eager to know other solutions..
Any way Thank you very much for ur suggestion

--
Chandu
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-11-28 : 02:55:50
or you can use sp_executesql and pass in the @txtValue as a parameter

SET @SQL =
'
select * from employees where @txtValue is NULL or ' + @drpSelectValue ' = @txtValue'
'



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -