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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to Execute Query in a stored procedure

Author  Topic 

sarathi125
Starting Member

3 Posts

Posted - 2010-08-18 : 03:20:20
Hi All,

I need to execute a sql query in my stored procedure.

That means I want to pass a SQL Query from my application to my stored procedure and need to execute it in the stored procedure.
The Query is a dynamic one, so each time the stored procedure have to write different values.

See the below sample and advise me the right thing to reach this.

Alter procedure CustomFilterQuery
@query varchar(max)
As
BEGIN
EXECUTE @query
END

Here i'm trying to execute the stored procedure but it throws error like
EXECUTE CustomFilterQuery 'select * from Orders'

Msg 2812, Level 16, State 62, Procedure CustomFilterQuery, Line 5
Could not find stored procedure 'select * from Orders'.

Shilpa22
Starting Member

37 Posts

Posted - 2010-08-18 : 03:24:22
try Exec(@query)

Thanks in Advance
Shilpa
Go to Top of Page

sarathi125
Starting Member

3 Posts

Posted - 2010-08-18 : 03:27:01
quote:
Originally posted by Shilpa22

try Exec(@query)

Thanks in Advance
Shilpa



Hi,
Yes, Its working fine.
Thanks for your time.

Is it possible to send the query with condition and values...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-08-18 : 04:11:01
you should also read this
The Curse and Blessings of Dynamic SQL


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

Go to Top of Page
   

- Advertisement -