Friday, March 30, 2012

Renaming a table

Hi,
Can someone please tell me the syntax for renaming a table in a query?
Thanks,
SteveHi Steve
I'm not sure what you mean by 'a table in a query'.
But perhaps you mean that you want to rename a table using a SQL command
instead of a GUI tool. In that case, use the sp_rename procedure:
EXEC sp_rename old_table_name, new_table_name
--
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Steve Caliendo" <scaliendo@.epion.com> wrote in message
news:ONP6RgzIFHA.572@.tk2msftngp13.phx.gbl...
> Hi,
> Can someone please tell me the syntax for renaming a table in a query?
> Thanks,
> Steve
>|||In a query? Queries don't rename tables. Use the sp_rename system SP:
EXEC sp_rename 'old_name', 'new_name', 'OBJECT'
If you mean you wan't to *alias* a table in a query, just use the AS
modifier:
SELECT A.* FROM YourTable AS A
David Portas
SQL Server MVP
--|||You can use stored procedure "sp_rename", but if the table is being
referenced inside a stored procedure, sp_rename will not update syscomments
table.
Example:
use northwind
go
create table dbo.t (
colA int
)
go
create procedure proc1
as
select * from dbo.t
go
exec sp_rename 't', 'tbl'
go
select
*
from
information_schema.tables
where
table_name = 'tbl'
go
select
routine_definition
from
information_schema.routines
where
routine_type = 'procedure'
and routine_name = 'proc1'
go
drop procedure proc1
go
drop table tbl
go
AMB
"Steve Caliendo" wrote:

> Hi,
> Can someone please tell me the syntax for renaming a table in a query?
> Thanks,
> Steve
>
>|||Do you mean changing the name of the table within the scope of the query?
Such as a named range or a table alias?
SELECT T1.*
FROM Table1 as T1
-Paul Nielsen
"Steve Caliendo" <scaliendo@.epion.com> wrote in message
news:ONP6RgzIFHA.572@.tk2msftngp13.phx.gbl...
> Hi,
> Can someone please tell me the syntax for renaming a table in a query?
> Thanks,
> Steve
>

No comments:

Post a Comment