Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Everybody

I am using stored procedure in my project. Now i want to implement search
function so, everytime where condition of my project will change. so, i pass where condition in storedprocedure.

e.g @Command=where firstname="shailesh" and lastname="prajapati


Stored procedure:
ALTER  Procedure [dbo].[madissues_searchAllMyFriends]
(
@UserId bigint,
@command varchar(200)
)
as
 begin
   select MIuser_id,firstname,lastname,profileimage from madissues_registration @command
  end

I am getting error... Please rely me...
Posted
Updated 29-Mar-11 18:11pm
v2

You can use EXECUTE to run a statement that's inside a string variable. See: http://msdn.microsoft.com/en-us/library/ms188332.aspx[^]

For example:
EXEUTE @command


So add the whole command to the variable and then use run it using EXEC.
 
Share this answer
 
v2
try this


create your query in code behind and pass it as string parameter in stored procedure
Query += "where UserMaster.UserId = " + UserId + " and OrderMaster.OrderDate >= '" + DateFrom + " " + DateTime.MinValue.ToLongTimeString() + "'";


in sql server

declare @Query varchar(150)
set @Query = 'Your Query'
exec ('select * from Table'+ @Query)
 
Share this answer
 
Hi Try This,

mahen25 is right but he give you the generalize approach but here i try to give you answer by considering your condition,

Just Add this line into your code,
@Command="where firstname='" + strFirstName + "' and lastname='" +strLastName +"'";

Here,
strFirstName is a String Variable which Holds your FirstName cuming From Any Control ( Ex:TextBOx like this String strFirstName=text1.Text )

And strLastName is a String Variable which Holds your LastName cuming From Any Control as explain above.

Then Just Call Your Stored Produre By passing @Command as parameter,

ALTER Procedure [dbo].[madissues_searchAllMyFriends]
(
@UserId bigint,
@command varchar(200)
)
as
begin
select MIuser_id,firstname,lastname,profileimage from madissues_registration @command
end


I hope this will help you
Enjoy
 
Share this answer
 
Hi pratik it's not working. If you have any idea please tell me immediately.
 
Share this answer
 
Hi jainmilan002,

Try the below code..

SQL
create procedure [dbo].[TestDynamic]
@name varchar(50)=''
as
declare @id int
set @id=2
Declare @strSql varchar(8000)
Set @strSql = ' '

Set @strSql = @strSql + 'select * from tblEvents'
If @name is not null
 Begin
  Set @strSql=@strSql+' where eventid=' + Cast(@id as Varchar)
 End
Else
Begin
 Set @strSql = @strSql
End
Exec (@strSql)


Hope it helps..

Thanks..
 
Share this answer
 
Use dynamic query in sql stored procedure

set @Query = 'Select * FROM TableName where ''+ @Approval +'' order By Trans_Date'


print @query
Execute sp_executesql @query
 
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