+ -
当前位置:首页 → 问答吧 → A表有个字段记录百分比 例如5% 现在需要一个SQL将B表的一个字段更新为(1-5%)

A表有个字段记录百分比 例如5% 现在需要一个SQL将B表的一个字段更新为(1-5%)

时间:2011-11-18

来源:互联网

A表有个字段per记录百分比 例如5% 现在需要一个SQL将B表的一个字段pero更新为(1-5%)

怎么写SQL呢 关联条件就是A和B有一个字段是相同的

作者: yangchengang1   发布时间: 2011-11-18

SQL code
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

SQL code

--纯粹的猜测

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

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 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

用户:yangchengang1 --> bambino2006
闭SQL大版1周
倒分贴:
http://topic.csdn.net/u/20111117/17/03533dcf-f337-4ec9-8c4a-e831fa6e2f7d.html


再倒分關1年

作者: roy_88   发布时间: 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 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

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%'

作者: dawugui   发布时间: 2011-11-18

引用 8 楼 dawugui 的回复:

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:弱弱的问一句,啥叫倒分帖?结贴时分谁都不给的意思?
引用 6 楼 roy_88 的回复:
用户: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

引用 7 楼 yangchengang111 的回复:
引用 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

引用 11 楼 ssp2009 的回复:

此贴均分


提示必须声明服务器: 消息 137,级别 15,状态 2,行 2
必须声明变量 '@B表'。
前边都执行成功了 SQL2000

作者: tianyazaixian   发布时间: 2011-11-26