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 2008 Forums
 Transact-SQL (2008)
 rename table variable column

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-07-16 : 03:24:23
Hi,
HOw is it possible to rename the field of a table variable in sql?
Thanks

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-16 : 04:49:52
Sorry, but no way, unless you declare another table variable (with new column name) and shift data to new one

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-07-16 : 05:15:35
ALTER, DROP, and TRUNCATE are not possible on a table variable

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-07-16 : 05:22:34
Is it possible to alter a field name of a temp table based on a variable?
i.e.
declare @Qrt smallint = 9
select field1, 'field' + @Qrt = field2 field3 from #tblMain
the result is the same except one of the field name is different.
The above will be:
field1, field9 field3

?
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-16 : 05:54:45
NOP, you can't rename a temporary table column

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-07-16 : 05:59:59
Thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-07-16 : 08:40:03
quote:
Originally posted by lionofdezert

NOP, you can't rename a temporary table column

--------------------------
http://connectsql.blogspot.com/


Why?

create table #t(empid int, firstname varchar(10))
GO
exec tempdb..sp_rename '#t.firstname','#t.first_name','column'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -