Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I made this procedure..

SQL
ALTER PROCEDURE [dbo].[MyProcedure]
            @pSelect nvarchar
AS
BEGIN
    SET NOCOUNT ON;

    select @pSelect from tabel1
END

I want to pass a select query like from c# code to this stored procedure

C#
MyProcedure("column1,column2");


How could I do this because stored procedure treat my parameter as a string and it behaves like

SQL
select N'column1,column2' from tabel1


pls help me

or provide a better option for this
Posted

Try:
SQL
EXEC ('SELECT ' + @pSelect + ' FROM Table1')


[edit]
But, be very careful - you need to vet @pSelect pretty carefully, as this is a good source of SQL injection attacks!
[/edit]
 
Share this answer
 
v2
Comments
Co. Aden 22-Sep-13 6:24am    
thanks it works
OriginalGriff 22-Sep-13 6:41am    
You're welcome!
Uses the Exec (Execute) statement.

SQL
ALTER PROCEDURE [dbo].[MyProcedure]
            @pSelect nvarchar
AS
BEGIN
    SET NOCOUNT ON;
    Exec('Select ' + @pSelect + ' from tabel1;');
END
 
Share this answer
 
v2

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