Sign-up....

请教这句sql的写法!急!

表1

zipcode stype

1301

1311

1321

1331

1392

1372

8602583

8602553

......

几百行,zipcode的长度不定

表二

phoneno

1378*******

1324*******

......

我想将表二中phoneno以表一中zipcode打头的行以stype替换掉,

替换后为:

表二

phoneno

28*******

14*******

这个update语句该怎么写呢?

小弟初学sql,请教各位

[240 byte] By [msdn] at [2007-10-1 16:51:04]
# 1 Re: 请教这句sql的写法!急!

用存储过程吧

declare

cursor ub is select * from 表1;

begin

for tab in ub loop

update 表二

set phoneno=replace(phoneno, tab.zipcode, tab.stype)

where instr(phoneno, tab.zipcode)=1

end loop;

end;

zzwind5 at 2005-10-19 10:45:59 >
# 2 Re: 请教这句sql的写法!急!

UPDATE tab2

SET phoneno = (SELECT REPLACE (phoneno, zipcode, stype)

FROM tab1

WHERE INSTR (phoneno, zipcode) = 1)

sasacat at 2005-10-19 10:51:00 >
# 3 Re: 请教这句sql的写法!急!

呵,楼上的别折腾了,还是我的办法最好...

sasacat at 2005-10-19 10:58:34 >
# 4 Re: 请教这句sql的写法!急!

用replace不好,如果137137137,就替换成222了

还是截串拼串的好,虽然很麻烦,但是准确

waterfirer at 2005-10-19 11:02:44 >
# 5 Re: 请教这句sql的写法!急!

晕...这我倒是没想到,你说这个replace为什么不多个参数表示替换第几个呢...

sasacat at 2005-10-19 11:13:40 >
# 6 Re: 请教这句sql的写法!急!

呵呵,等你下回加进去呢。:)

猫,快上qq,上回的导入问题还有事情要问你。

waterfirer at 2005-10-19 11:18:47 >
# 7 Re: 请教这句sql的写法!急!

呵呵,晚了

:)

bzszp at 2005-10-19 11:19:33 >
# 8 Re: 请教这句sql的写法!急!

不过功夫不负有心人,偶又写了一个:)

UPDATE tab2

SET phoneno =

(SELECT stype

|| SUBSTR (phoneno, LENGTH (zipcode) + 1,

LENGTH (phoneno) + 1)

FROM tab1

WHERE INSTR (phoneno, zipcode) = 1);

sasacat at 2005-10-19 11:23:33 >
# 9 Re: 请教这句sql的写法!急!

呵呵,这个是经验,遇到多了自然会明白,因为不能用replace本来打算用ltrim()的,后来发现还是不对,为什么ltrim()替换的是第2个参数的集合,真奇怪……

SQL> select ltrim(13717311732137,137) from dual;

LTRIM(13717311732137,137)

----

2137

duanzilin at 2005-10-19 11:24:05 >
# 10 Re: 请教这句sql的写法!急!

晕...原来已经有人写了

sasacat at 2005-10-19 11:25:23 >
# 11 Re: 请教这句sql的写法!急!

谢谢各位,吃午饭先,过会儿来看!

ericbu at 2005-10-19 11:43:41 >
# 12 Re: 请教这句sql的写法!急!

接分吧各位!

ericbu at 2005-10-19 13:16:03 >

Oracle

All Classified