Sending dynamic fields to a store procedure

M

mazdotnet

Hi,

I have a C# web app that searches my database table using the
following search parameters

Search string, criteria (> < =) and the field you want to perform your
search on. My understanding is that stored procedure is the way to go.
What's the best way of doing this using stored procedures. Can I
define a placeholder for the field name?

Ex.
SELECT field1, field2... FROM Table WHERE field1='value1' where field1
and value1 are both sent from code.

If it's not possible then what is the best way to approach this
problem? I see so many searches like that on the internet. I can only
do them with inline SQL and not stored procedure.

Thank you
Maz.
 
G

Guest

Hi,

you can use following code in stored procedure

Input Parameter
@Table_Name varchar(100),
@Comma_Seperated_Fields as varchar(1000),

declare @Query as nvarchar(4000)
set @query = 'Select ' + @Comma_Seperated_Fields + ' from ' + @Table_Name
EXECUTE sp_executesql @query

Cheers
Chetan Chaphekar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top