Showing posts with label syntax. Show all posts
Showing posts with label syntax. Show all posts

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
>

Wednesday, March 21, 2012

rename column

i need t-sql syntax for renaming a column.
the column name contains the [] characters.
alter table <tablename> rename [old field name] to NewFieldName
isn't working for me.
tia,
mcnewsxpthis works:
EXEC sp_rename 'MyTable.[Old Field]', 'NewField', 'COLUMN'|||use sp_rename..
I haven't seen the sytax that you have used.|||You might want to check this out ::
http://vadivel.blogspot.com/2004/08...g-sprename.html
Best Regards
Vadivel
http://vadivel.blogspot.com
"mcnews" wrote:

> i need t-sql syntax for renaming a column.
> the column name contains the [] characters.
> alter table <tablename> rename [old field name] to NewFieldName
> isn't working for me.
> tia,
> mcnewsxp
>sql