好难啊好难!三个表的问题!

love_study 2006-11-25 07:47:37
我有3个表:A(ID,NO1,NO2,NO3) B(ID,NUM,RENO) C(FREENO)
将A表的数据根据条件导入到 B表和C表
要求:1、在表A中存在与[NOx](x为1,2,3相应数字)相同的情况,就直接将[NOx]
所在行数据的ID和NOx插入到B表的ID和RENO中。
2、如果第1步,没有相应的数据就把A表中[NOx]的数据第三位的‘-’去掉后
相应的值在A表ID中是否存在,如果有就将将[NOx] 所在行数据的ID和NOx
插入到B表的ID和RENO中。
3、如果第2步,仍找不到,就将那些数据的ID值传入到C表中的FREENO中(C表
已经有其他字段,并且有数据)
4、表B中NUM实现自动增加的功能,初始为1.
这个导入的工作,能否用sql语句实现,如果能,请大家帮助,共同探讨!谢谢!!

...全文
197 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
新鲜鱼排 2006-11-26
  • 打赏
  • 举报
回复
解决了第一个问题。
后面的问题不是很明白。
create table A(ID int,NO1 int,NO2 int,NO3 int)
insert A select 1,1,1,1
insert A select 2,4,3,2
insert A select 3,1,2,3
create table B(NUM int identity(1,1),ID int,RENO int)

insert B
select ID,NO1
from
(select ID,
case NO1 when 1 then 1 else 0 end [NO1],
case NO2 when 2 then 2 else 0 end [NO2],
case NO3 when 3 then 3 else 0 end [NO3]
from A
where NO1=1 or NO2=2 or NO3=3) a
where NO1<>0
union all
select ID,NO2
from
(select ID,
case NO1 when 1 then 1 else 0 end [NO1],
case NO2 when 2 then 2 else 0 end [NO2],
case NO3 when 3 then 3 else 0 end [NO3]
from A
where NO1=1 or NO2=2 or NO3=3) a
where NO2<>0
union all
select ID,NO3
from
(select ID,
case NO1 when 1 then 1 else 0 end [NO1],
case NO2 when 2 then 2 else 0 end [NO2],
case NO3 when 3 then 3 else 0 end [NO3]
from A
where NO1=1 or NO2=2 or NO3=3) a
where NO3<>0

select * from B

drop table A
drop table B
/*
NUM ID RENO
----------- ----------- -----------
1 1 1
2 3 1
3 3 2
4 3 3

(4 行受影响)
*/
love_study 2006-11-25
  • 打赏
  • 举报
回复
如果用程序作,PB的话,又没有什么好的设计方法,希望大家提供!

22,210

社区成员

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

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