请问一个方案

daohua 2013-12-04 03:10:34
我现在有100张表,表结构不同。这些表中都有一个字段是 code ,VARCHAR型的。
我想将100张表的所有code 值都在中间第二位插入一个字符的1.

例如1000111010,在第二位插入一个1则为,11000111010

请问有什么好的办法或语句实现啊。
这100张表我可以查出来。
...全文
137 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
KeepSayingNo 2013-12-04
  • 打赏
  • 举报
回复
引用 4 楼 daohua 的回复:
请问楼上,如果表明是不有规律的,而是存储在一个系统表里呢。 也就是表A里存了100个表的表名,那代码应该如何改呢
这就要使用游标

declare @current_tablename varchar(256)

exec('declare my_cursor1 cursor for select distinct tablename from A')
open my_cursor1
declare @tablename sysname
fetch next from my_cursor1 into @tablename
	while(@@fetch_status= 0)
		begin	
			set @current_tablename =convert(varchar(256),@tablename)
			fetch next from my_cursor1 into @tablename
		end
close my_cursor1
deallocate my_cursor1
KeepSayingNo 2013-12-04
  • 打赏
  • 举报
回复
CSDN真挫,我想用红色标注,结果显示不出来

update test set code=SUBSTRING(code,1,1)+'1'+SUBSTRING(code,2,len(code))
KeepSayingNo 2013-12-04
  • 打赏
  • 举报
回复
对一楼的写法进行改进

create table test(code nvarchar(10))
insert into test values('11110')
insert into test values('12110')
insert into test values('13110')
insert into test values('14110')
insert into test values('15110')
update test set code=SUBSTRING(code,1,1)+'1'+SUBSTRING(code,2,len(code))
/*
111110
112110
113110
114110
115110
*/
daohua 2013-12-04
  • 打赏
  • 举报
回复
请问楼上,如果表明是不有规律的,而是存储在一个系统表里呢。 也就是表A里存了100个表的表名,那代码应该如何改呢
KeepSayingNo 2013-12-04
  • 打赏
  • 举报
回复

declare @index int 
declare @tablename varchar(256)
set @index = 1
while @index<=100
begin
	set @tablename = 'A'+@index
	--写访问表和插入数据的代码
	set @index= @index+1
end
daohua 2013-12-04
  • 打赏
  • 举报
回复
请问 能举例说的动态循环的做法吗 。例如有10个表分别为A1 A2 A3 A4
  • 打赏
  • 举报
回复

create table test(code nvarchar(10))
insert into test values('11110')
insert into test values('12110')
insert into test values('13110')
insert into test values('14110')
insert into test values('15110')
update test set code=SUBSTRING(code,1,1)+'1'+SUBSTRING(code,2,1000)
/*
111110
112110
113110
114110
115110
*/
单表的 创建新表 存 表中有code字段的表名 用动态循环执行更新

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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