免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: bbniu
打印 上一主题 下一主题

关于sql语法的问题 [复制链接]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
11 [报告]
发表于 2007-03-12 23:00 |只看该作者
原帖由 lklgdkp 于 2007-3-7 22:51 发表
with c as
(select a1 from a,b where a.p1 = b.p1 and a.p2 = b.p2)
update c set a1=1

A common table expression can be thought of as a temporary result set that is defined within the execution sc ...




刚刚实验了一下,这种方法还真的不行。

论坛徽章:
0
12 [报告]
发表于 2007-03-13 02:28 |只看该作者
咦?!

这个句子不应该那么费劲吧?!


  1. update  a set a1='1' where (p1,p2) in (select b.p1,b.p2 from b where a.p1=b.p1 and a.p2=b.p2);
复制代码

论坛徽章:
0
13 [报告]
发表于 2007-03-13 21:04 |只看该作者
原帖由 lklgdkp 于 2007-3-7 22:51 发表
with c as
(select a1 from a,b where a.p1 = b.p1 and a.p2 = b.p2)
update c set a1=1

A common table expression can be thought of as a temporary result set that is defined within the execution sc ...


这样写可以吗?

with c as
(select a1 as c1 from a,b where a.p1 = b.p1 and a.p2 = b.p2)
update c set c1=1

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
14 [报告]
发表于 2007-03-13 22:39 |只看该作者
应该也不行。

我今天做了个实验:

http://www.loveunix.net/discuz/v ... &extra=page%3D1

论坛徽章:
0
15 [报告]
发表于 2007-03-14 07:15 |只看该作者
[quote]原帖由 beginner-bj 于 2007-3-12 23:00 发表

update
(select a1 from a,b where a.p1 = b.p1 and a.p2 = b.p2) c
set a1=1
也不行吗?

我是觉得写sql要尽量避免in表之类的

论坛徽章:
0
16 [报告]
发表于 2007-03-14 10:33 |只看该作者
原帖由 raullew 于 2007-3-14 07:15 发表
[quote]原帖由 beginner-bj 于 2007-3-12 23:00 发表

update
(select a1 from a,b where a.p1 = b.p1 and a.p2 = b.p2) c
set a1=1
也不行吗?

我是觉得写sql要尽量避免in表之类的


试了不行。

db2 => update (select a1 from a,b where a.p1 = b.p1 and a.p2 = b.p2) c set a1=1

DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0150N  The target fullselect, view, typed table, materialized query table,
or staging table in the INSERT, DELETE, UPDATE, or MERGE statement is a target
for which the requested operation is not permitted.  SQLSTATE=42807

论坛徽章:
0
17 [报告]
发表于 2007-03-14 10:37 |只看该作者
俺的也不行吗?

刚刚测试了一下,没问题啊!!

db2 => select * from taba

A1   P1   P2
---- ---- ----
12   pp   xx
32   pp   xx
44   px   px
56   pb   up
4    ty   px
9    qu   rt

  6 条记录已选择。

db2 => select * from tabb

P1   P2
---- ----
er   pe
pp   xx
pp   xx
px   px
pb   up
sd   nj

  6 条记录已选择。

db2 => update  taba set a1='1' where (p1,p2) in (select tabb.p1,tabb.p2 from tab
b where taba.p1=tabb.p1 and taba.p2=tabb.p2)
DB20000I  SQL 命令成功完成。
db2 => select * from taba

A1   P1   P2
---- ---- ----
1    pp   xx
1    pp   xx
1    px   px
1    pb   up
4    ty   px
9    qu   rt

  6 条记录已选择。

db2 =>


是这个意思吗?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
18 [报告]
发表于 2007-03-14 12:20 |只看该作者
看来结论是:update语句不支持使用common table expression的方式

论坛徽章:
0
19 [报告]
发表于 2007-03-14 17:42 |只看该作者
原帖由 beginner-bj 于 2007-3-14 12:20 发表
看来结论是:update语句不支持使用common table expression的方式


UPDATE的对象好像只能是table或updateable view吧?!应该不能是common table !

论坛徽章:
0
20 [报告]
发表于 2007-03-14 21:19 |只看该作者
原帖由 mymm 于 2007-3-14 17:42 发表


UPDATE的对象好像只能是table或updateable view吧?!应该不能是common table !


我刚才也试了一下,确实无法对join的集合进行update,i惭愧啊。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP