Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: StoredProcedures
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?"
Posted 19-Sep-12 3:17am
Edited 19-Sep-12 16:27pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
JOEY G. MOYA at 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)

  Print Answers RSS
0 DamithSL 325
1 OriginalGriff 199
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 123
0 OriginalGriff 7,854
1 DamithSL 5,949
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,084
4 Kornfeld Eliyahu Peter 4,539

Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100