记录修改问题,送100分

tanyaliji 2003-08-18 07:04:42
现有库,有120多个表,有多个记录 字段为“山东0531XXX”现改为“加州0531XXX”
大约修改90多个表。(注表中可能有多个字段有“山东”要求全部修改)
...全文
53 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanyaliji 2003-08-20
  • 打赏
  • 举报
回复
好像明白点
CrazyFor 2003-08-19
  • 打赏
  • 举报
回复
思路:

游标循环所有表的所有字段,
tanyaliji 2003-08-19
  • 打赏
  • 举报
回复
谢谢
pengdali 2003-08-19
  • 打赏
  • 举报
回复
如果你是要改你的表里的数据,而不是改列名:

declare cursor1 cursor for select d.name,a.name from syscolumns a, systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and b.name like '%char'
declare @i varchar(100),@j varchar(100),@f varchar(100)
open cursor1
fetch cursor1 into @i,@j
while @@fetch_status=0
begin
set @f='加州'+right(@j,len(@j)-2)
exec('update '+@i+' set '+@j+'=''加州''+right('+@j+',len('+@j+')-2) where '+@j+' like ''山东0531%''')
fetch cursor1 into @i,@j
end
close cursor1
deallocate cursor1
suowei11 2003-08-19
  • 打赏
  • 举报
回复
sdf
pengdali 2003-08-19
  • 打赏
  • 举报
回复
declare cursor1 cursor for select d.name,a.name from syscolumns a,sysobjects d where a.id=d.id and d.xtype='U' and a.name like '山东0531%'
declare @i varchar(100),@j varchar(100),@f varchar(100)
open cursor1
fetch cursor1 into @i,@j
while @@fetch_status=0
begin
set @f='加州'+right(@j,len(@j)-2)
exec('sp_rename '''+@i+'.'+@j+''','''+@f+'''')
fetch cursor1 into @i,@j
end
close cursor1
deallocate cursor1
pengdali 2003-08-19
  • 打赏
  • 举报
回复
A. 重命名表
下例将表 customers 重命名为 custs。

EXEC sp_rename 'customers', 'custs'

B. 重命名列
下例将表 customers 中的列 contact title 重命名为 title。

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'

pengdali 2003-08-19
  • 打赏
  • 举报
回复
上面不是答案吗??
你不是要把“山东0531XXX”字段名 改为“加州0531XXX 字段名吗?

游标:

declare cursor_insert cursor for select c# from cs
declare @i int
open cursor_insert
fetch cursor_insert into @i
while @@fetch_status=0
begin
print @i
fetch cursor_insert into @i
end
close cursor_insert
deallocate cursor_insert
tanyaliji 2003-08-19
  • 打赏
  • 举报
回复
游标怎么写啊
benxie 2003-08-18
  • 打赏
  • 举报
回复
互相学习
hjb111 2003-08-18
  • 打赏
  • 举报
回复
declare @s varchar(20)
set @s=''
declare cur_a cursor for select a.name from sysobjects a,syscolumns b where a.id=b.id and b.name like'山东0531%'
open cur_a
fetch next from cur_a into @s
while @@fetch_status<>0
begin
exec('sp_rename '+@s+','+replace(@s,'山东','加州')+',''COLUMN''')
fetch next from cur_a into @s
end
close cur_a
deallocate cur_a
hjb111 2003-08-18
  • 打赏
  • 举报
回复
不一定能通过,思路已经有了,楼主可以自已修改一下!
hjb111 2003-08-18
  • 打赏
  • 举报
回复
declare @s varchar(20)
set @s=''
declare cur_a cursor for select a.name from sysobjects a,syscolumns b where a.id=b.id and b.name like'山东0531%'
open a
fetch next from cur_a into @s
while @@fetch_status<>0
begin
exec('sp_rename '+@s+','+replace(@s,'山东','加州')+',''COLUMN''')
fetch next from cur_a into @s
end
close cur_a
deallocate cur_a
pengdali 2003-08-18
  • 打赏
  • 举报
回复
declare cursor1 cursor for select d.name,a.name from syscolumns a,sysobjects d where a.id=d.id and d.xtype='U' and a.name like '山东0531%'
declare @i varchar(100),@j varchar(100),@f varchar(100)
open cursor1
fetch cursor1 into @i,@j
while @@fetch_status=0
begin
set @f='加州'+right(@j,len(@j)-2)
print('sp_rename '''+@i+'.'+@j+''','''+@f+'''')
fetch cursor1 into @i,@j
end
close cursor1
deallocate cursor1

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧