方法:EXEC sp_rename
例子:
declare @arrID int
declare @tempTableName varchar(20)
declare @tempId int
declare @tempName varchar(20)
declare @name varchar(100)
declare @name2 varchar(100)
declare @arrTable table(tableid int identity(1,1) not null, tablename varchar(100) not null)
--设置要修改的表
insert into @arrTable(tablename) values('[DOM_REL_BAIKE2]')
insert into @arrTable(tablename) values('[DOM_REL_SEARCH]')
insert into @arrTable(tablename) values('[DOM_REL_Lunzhu2]')
insert into @arrTable(tablename) values('[DOM_REL_SUBJECT]')
--select * from @arrTable
declare @tableVar table(id int identity(1,1) not null, fieldname varchar(100) not null)
WHILE EXISTS(select tableid from @arrTable)
BEGIN
select top 1 @arrID = tableid,@tempTableName=tablename from @arrTable
print 'tablename:----'+@tempTableName
delete from @arrTable where tableid = @arrID
--设置要修改的字段
insert into @tableVar(fieldname) values('LANUAGE')
insert into @tableVar(fieldname) values('WORDNUM')
insert into @tableVar(fieldname) values('CONTENT_CAT')
insert into @tableVar(fieldname) values('REL_WORD')
insert into @tableVar(fieldname) values('BOOK_PAGE')
insert into @tableVar(fieldname) values('CATOGERY')
insert into @tableVar(fieldname) values('BOOK_PAGE')
insert into @tableVar(fieldname) values('CONTENT_CATID')
insert into @tableVar(fieldname) values('CONTENT_CASCADID')
insert into @tableVar(fieldname) values('CATOGERYID')
insert into @tableVar(fieldname) values('CATOGERY_CASCADID')
insert into @tableVar(fieldname) values('SUB_TITLE')
insert into @tableVar(fieldname) values('TITLE_EN')
insert into @tableVar(fieldname) values('SUBTITLE_EN')
insert into @tableVar(fieldname) values('WRITETIME')
insert into @tableVar(fieldname) values('PUBDATE')
insert into @tableVar(fieldname) values('BOOKCODE')
insert into @tableVar(fieldname) values('DOWNCOUNT')
insert into @tableVar(fieldname) values('VIEWCOUNT')
--select * from @tableVar
WHILE EXISTS(select id from @tableVar)
begin
select top 1 @tempId = id,@tempName=fieldname from @tableVar
delete from @tableVar where id = @tempId
--指定要改字段名的表名
select @name = @tempTableName + '.[' + @tempName + ']'
select @name2 = @tempTableName + '.[TEMPCOLUMN]'
print 'Name:----'+@name
print 'Name:----'+@name2
EXEC sp_rename @name, 'TEMPCOLUMN', 'COLUMN'
EXEC sp_rename @name2, @tempName, 'COLUMN'
end
END