一个非常非常辣手又古老的问题!!

gxbhhp 2003-08-30 01:13:02
如何把下面相对应的编号和城市名称一一插到一个新表中去?
绝对注意:一定要考虑到变量的长度问题!!
比如:declare @string varchar(8000)才8000个字一定是放不下的。
如:我有一个表叫:pri

idd code cname
1 '01','0101','0102','0103' 北京','东城区','西城区'

把它分成:

idd code cname
1 01 北京
2 0101 东城区
3 0102 西城区

所以,就不能通过把它分成两个文件:a.txt b.txt的方式。
直接通过从上表pri搜索出来,放到下表中去,如何做?

...全文
42 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyun2003 2003-09-01
  • 打赏
  • 举报
回复
学习
gxbhhp 2003-09-01
  • 打赏
  • 举报
回复
: zjcxc(邹建) ,不行哪。。

(所影响的行数为 8000 行)

服务器: 消息 207,级别 16,状态 3,行 11
Invalid column name 'code'.
服务器: 消息 207,级别 16,状态 1,行 11
Invalid column name 'code'.
服务器: 消息 207,级别 16,状态 1,行 11
Invalid column name 'code'.
服务器: 消息 207,级别 16,状态 1,行 17
Invalid column name 'cname'.
服务器: 消息 207,级别 16,状态 1,行 17
Invalid column name 'cname'.
服务器: 消息 207,级别 16,状态 1,行 17
Invalid column name 'cname'.
zjcxc 2003-08-30
  • 打赏
  • 举报
回复
用下面的方法就行了.

--为数据分析准备临时表
select top 8000 id=identity(int,1,1) into #temp
from(select id from syscolumns) a,
(select id from syscolumns) b,
(select id from syscolumns) c

--分拆代码
select idd=identity(int,1,1)
,code=substring(a.code,b.id+1,charindex(',',a.code+',',b.id)-b.id-2)
into #tb1 from pri a,#temp b
where substring(','+a.code,b.id,1)=','

--分拆名称
select idd=identity(int,1,1)
,cname=substring(a.cname,b.id+1,charindex(',',a.cname+',',b.id)-b.id-2)
into #tb2 from pri a,#temp b
where substring(','+a.cname,b.id,1)=','

--合并数据
insert into 接受数据的表
select a.idd,a.code,b.cname
from #tb1 a inner join #tb2 b on a.idd=b.idd

--删除临时表
drop table #temp,#tb1,#tb2

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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