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 2000 Forums
 Transact-SQL (2000)
 Use variable as column Name

Author  Topic 

anj755
Starting Member

8 Posts

Posted - 2007-04-23 : 14:23:13
Is it possible in T-SQL, specifically SQL Server 2005 to do


select ID as @Persontype, Number ,Date
from Table

the reason I need it as a variable is that it is in a stored proc and according to where the proc is called I want to name the column as such.

like if it is an Applicant then the result set would be

Applicant Number Date

if it is Customer then the result set column names would be

Customer Number Date



dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-04-23 : 14:25:22
I think this kind of customization should be done at the front end? otherwise you'e end up creating problems at the DB level..


************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

anj755
Starting Member

8 Posts

Posted - 2007-04-23 : 16:10:46
No This is a sproc that I need to use in my SSIS package, so it is something i need to be able to do in my sproc
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-23 : 16:16:44
See http://www.sommarskog.se/dynamic_sql.html


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-23 : 16:24:54
I am just amazed sometimes at how people insist on making simple things so hard on themselves....

WHY do you feel that this column should be renamed based on the export? Can you give an example of how this @persontype parameter is assigned and how this is executed? Where does this data end up, and who ends up consuming it?

It really sounds like you either have a really, really bad database design and/or you are trying to do too much all at once to cover all possibilities too generically. Either way, I can almost guarantee that you are making something that should be simple overly complicated.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-23 : 16:34:23
"Take a walk on the dark side"


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

anj755
Starting Member

8 Posts

Posted - 2007-04-23 : 16:49:00
i have a table that has personId three times, once as Customer, once as Applicant and once as Owner
all of them have PersonID which then can map to their phonenumber, address etc

now I need to make a select statement so that customer have their set of data, Applicant have their own and Owner have their own. If I can make a function that takes in the PersonID and what type then it'll return me the desired date with desired column name
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-23 : 16:56:49
A single function or stored proc should NOT have varying column names returned -- in a relational database, tables and columns are constants, not things that get altered dynamically from time to time. If I want to use your stored procedure, how can I write more code or bind reports or things to it if sometimes the column name returned changes?

In fact, if I pass in @PersonType of "Customer" when calling that stored proc, don't I KNOW that customers are being returned since that is what I asked for? Why do you feel that the column name needs to change to reflect this? Why not just leave it as is, and let whoever is calling this stored procedure label the resulting data any way it wants?

I really hope this makes sense. If you want data specifically for Customers or Applicants returned, and column names labelled appropriately, then you should have different stored procedures or functions that you would call, not all of them mashed up into 1.

In other words, you can't have it both ways: either have 1 generic stored proc that returns generic data and generic labels, or have specific stored procs that return specific results. Mixing the two up, while technically possible, is a poor design and really complicates your code and your design.

Just my $.02

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -