A表有个字段记录百分比 例如5% 现在需要一个SQL将B表的一个字段更新为(1-5%)
时间:2011-11-18
来源:互联网
怎么写SQL呢 关联条件就是A和B有一个字段是相同的
作者: yangchengang1 发布时间: 2011-11-18
update b set pero=a.per from a join b on a.id=b.id where a.per between %1 and 5%
作者: fredrickhu 发布时间: 2011-11-18
--纯粹的猜测 update b set pero=a.per from a join b on a.id=b.id where a.per between '%1' and '5%'
作者: fredrickhu 发布时间: 2011-11-18
作者: fredrickhu 发布时间: 2011-11-18
declare @A表 table (id int,per varchar(4)) insert into @A表 select 1,'5%' union all select 2,'15%' union all select 3,'85%' declare @B表 table (id int,pero varchar(4)) insert into @B表 select 1,null union all select 2,null union all select 3,null UPDATE @B表 SET pero = LTRIM(100 - REPLACE(a.per, '%', '')) + '%' FROM @B表 b LEFT JOIN @A表 a ON b.id = a.id SELECT * FROM @B表 /* id pero ----------- ---- 1 95% 2 85% 3 15% */
作者: maco_wang 发布时间: 2011-11-18

作者: jwdream2008 发布时间: 2011-11-18
闭SQL大版1周
倒分贴:
http://topic.csdn.net/u/20111117/17/03533dcf-f337-4ec9-8c4a-e831fa6e2f7d.html
再倒分關1年
作者: roy_88 发布时间: 2011-11-18
SQL code
declare @A表 table (id int,per varchar(4))
insert into @A表
select 1,'5%' union all
select 2,'15%' union all
select 3,'85%'
declare @B表 table (id int,pero varchar(4))
insert into @B表
select……
ERROR: An operand of an arithmetic operation or an operand of
a function that requires a number is not a number.
DB2 SQL
Error: SQLCODE=-402, SQLSTATE=42819, SQLERRMC=-, DRIVER=3.57.82
Error
Code: -402
还要考虑值为空的情况 如果为空就不更新
作者: yangchengang111 发布时间: 2011-11-18
怎么写SQL呢 关联条件就是A和B有一个字段是相同的
-->
update b set pero = '1-5%' from a , b where a.id = b.id and b.per = '5%'
作者: dawugui 发布时间: 2011-11-18
A表有个字段per记录百分比 例如5% 现在需要一个SQL将B表的一个字段pero更新为(1-5%)
怎么写SQL呢 关联条件就是A和B有一个字段是相同的
-->
update b set pero = '1-5%' from a , b where a.id = b.id and b.per = '5%'
不能直接写 要带参数的
作者: yangchengang111 发布时间: 2011-11-18
PS:弱弱的问一句,啥叫倒分帖?结贴时分谁都不给的意思?
用户:yangchengang1 --> bambino2006
闭SQL大版1周
倒分贴:
http://topic.csdn.net/u/20111117/17/03533dcf-f337-4ec9-8c4a-e831fa6e2f7d.html
再倒分關1年
作者: geniuswjt 发布时间: 2011-11-18
作者: ssp2009 发布时间: 2011-11-18
引用 4 楼 maco_wang 的回复:
SQL code
declare @A表 table (id int,per varchar(4))
insert into @A表
select 1,'5%' union all
select 2,'15%' union all
select 3,'85%'
declare @B表 table (id int,pero var……
在sql server 中这样是没有错误的
SQL code
declare @A表 table (id int,per varchar(4)) insert into @A表 select 1,'5%' union all select 2,'15%' union all select 3,'85%' UNION ALL SELECT 4,'' UNION ALL SELECT 6,null declare @B表 table (id int,pero varchar(4)) insert into @B表 select 1,null union all select 2,null union all select 3,NULL UNION ALL SELECT 4,NULL UNION ALL SELECT 5,NULL UNION ALL SELECT 6,null UPDATE @B表 SET pero = LTRIM(100 - REPLACE(a.per, '%', '')) + '%' FROM @B表 b LEFT JOIN @A表 a ON b.id = a.id SELECT * FROM @B表 /* id pero ----------- ---- 1 95% 2 85% 3 15% 4 100% 5 NULL 6 NULL */
你是放在DB2里运行的吗?
作者: maco_wang 发布时间: 2011-11-18
此贴均分
提示必须声明服务器: 消息 137,级别 15,状态 2,行 2
必须声明变量 '@B表'。
前边都执行成功了 SQL2000
作者: tianyazaixian 发布时间: 2011-11-26
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28