Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please allow me to illustrate my question. I have a table called employee with the columns empid, last, first, mid, address and city. I've cloned this table to employeecl with the columns empid, last and first.
In my storeprocedure I want to select employeecl(empid,last,first) from employee so the next time I want to add a field to be selected in my SP I just have to add it in my clone table without anymore editing my SP, meaning columns that will be specified in my SP will depend on the columns available in the clone table which is also available in the source table. My cloned table does not contain values only fields. Do you think this is possible if so please help me I will use this in my project as I'm assigned to make our SPs dynamically.

Your help would be a great help.

Thank you!

In my question above iam using sql server 2005 actually i just want to make my stored procedure dynamic meaning i want to create select statement inside sp "Can i store fields in a table no records just fields and use that tablefields in my select statement?"
Updated 19-Sep-12 15:27pm

What you should do here is use the Create View[^] feature. In case you're not using a MS SQL DBMS flavor, this type of construct is supported by all major RDMBs.


— Manfred
Share this answer
for example

declare @mainquery varchar(max);
declare @subquery varchar(max);

set @subquery = 'name , address';
set @mainquery= 'select id,'+ cast(@subquery as varchar )+' from table' ;

exec sp_executesql @mainquery
Share this answer
JOEY G. MOYA 21-Sep-12 1:16am    
Think this solution would help me Thank you very much

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