edrp.cn的Blog

学习,需要交流,欢迎大家和我共同来学习C#,ASP.NET,MS SQL Server开发Web项目,欢迎大家和我交流

博客园 首页 新随笔 联系 订阅 管理

--修改所有Detail Table 字段为Not in 'D' Name
declare @TableName varchar(50),@FielName varchar(50),@TableID varchar(20),@FieldID varchar(20),
@Table_Description varchar(200),@Field_Description varchar(200),
@SQL varchar(1000)

Declare selectTableNmae cursor for
SELECT o.name, c.name fieldname
FROM syscolumns c
INNER JOIN sysobjects o ON c.id = o.id
WHERE o.type = 'U' and LEFT(o.Name,2)='T_'
and RIGHT(o.Name,6)='Detail'
and SUBSTRING(o.Name, 3, len(o.Name)-8)=SUBSTRING(c.name, 2, len(c.name)-3)
and left(c.name,1)='D' and RIGHT(c.name,2)='ID'
ORDER BY o.name desc, c.name

Open selectTableNmae
select @TableName='',@FielName=''
Fetch next from selectTableNmae into @TableName,@FielName
While @@Fetch_Status = 0
begin
--print @TableName
--print @FielName
--EXEC sp_rename '表名.旧字段名', '新字段名', 'COLUMN'
select @SQL='EXEC sp_rename '''+@TableName+'.'+@FielName+''','''+ SUBSTRING(@FielName, 2, len(@FielName)-1)+''',''COLUMN'''
--print @SQL
Exec(@SQL)
Fetch next from selectTableNmae into @TableName,@FielName
end
Close selectTableNmae
DeAllocate selectTableNmae

posted on 2023-07-05 22:35  edrp.cn  阅读(8)  评论(0编辑  收藏  举报