求一字符串处理的方法

sxmonsy 2009-05-16 03:15:31
在字段A中保存头5回字段B的修改值.字段B只有一个数字。要是B的值在A中已经有了就不更改。
在SQL2000下操作。触发器或存储过程都行。
例子:
当A为空时:''
更改B值为1时;
更改A值为:"1"

更改B值为2时;
更改A值为:"2,1"

当A为:'1,2,3,4,5'
更改字段B为:6
A改为:'6,1,2,3,4'

更改字段B为:7
A改为:'7,6,1,2,3'

更改字段B为:1
A中有1不更改:'7,6,1,2,3'


更改字段B为:10
A更改:'10,7,6,1,2'
...全文
212 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxmonsy 2009-05-21
  • 打赏
  • 举报
回复
谢谢楼上在凌晨的回贴。我马上结贴
Limpire 2009-05-21
  • 打赏
  • 举报
回复
--> 如果UPDATE字段B 并且新值在字段A中没有
if update(b) and not exists(select 1 from ta a,inserted b where b.id=a.id and charindex(','+ltrim(b.b)+',',','+a.a+',')>0)
begin
update ta set a =
--> 判断字段A的逗号数目
case len(a.a)-len(replace(a.a,',',''))
--> 4个逗号(五项):新值+','+前4项
when 4 then ltrim(b.b) + ',' + reverse(stuff(reverse(a.a),1,charindex(',',reverse(a.a)),''))
--> 否则:新值+','+字段A
else ltrim(b.b)+isnull(','+a.a,'')
end
from ta a,inserted b where a.id=b.id
end
-无-为- 2009-05-18
  • 打赏
  • 举报
回复
学习了
sxmonsy 2009-05-18
  • 打赏
  • 举报
回复
已经成功了。不过没明白语句的意思。还望讲解下
wanshichen 2009-05-18
  • 打赏
  • 举报
回复
用触发器,16楼的应该就可以
sxmonsy 2009-05-18
  • 打赏
  • 举报
回复
谢谢楼上的,有点没看明白,能方便讲下吗?
pt1314917 2009-05-17
  • 打赏
  • 举报
回复
create table ta(id int identity(1,1),a varchar(50),b int)
go

create trigger tri_ta on ta for insert,update
as
if not exists(select 1 from ta a,inserted b where b.id=a.id and charindex(','+ltrim(b.b)+',',','+a.a+',')>0)
and exists(select 1 from ta a,inserted b where b.id=a.id and (len(a.a)-len(replace(a.a,',',''))<4 or isnull(a.a,'')=''))
update ta set a=ltrim(b.b)+isnull(','+a.a,'') from ta a,inserted b where a.id=b.id
go

--测试:
insert into ta(b) select 1
select * from ta

update ta set b=5 where id=1
select * from ta

update ta set b=5 where id=1
select * from ta

update ta set b=4 where id=1
select * from ta

update ta set b=3 where id=1
select * from ta


update ta set b=64 where id=1
select * from ta

update ta set b=8 where id=1
select * from ta

--删除测试
drop table ta
lihan6415151528 2009-05-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ks_reny 的回复:]
SQL code
create trigger up_tb
on tb
for update
as
begin
if exists(select 1 from inserted where a='') -----a为空的情况
begin
update a
set a.A=(case when b.B=1 then '1'
when b.B=2 then '1,2'
else null end)
from tb a, inserted b
where a.id=b.id
end
else
begin
......…
[/Quote]

学习
claro 2009-05-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 fredrickhu 的回复:]
引用 11 楼 ks_reny 的回复:
SQL code
create trigger up_tb
on tb
for update
as
begin
if exists(select 1 from inserted where a='')  -----a为空的情况
begin
update a
set a.A=(case when b.B=1 then '1'
when  b.B=2 then '1,2'
else null end)
from tb a, inserted b
where a.id=b.id
end
else
begin
......…


学习了 谢谢 我不怎么懂触发器
[/Quote]慢慢就懂了。
Limpire 2009-05-17
  • 打赏
  • 举报
回复
借楼上的改改:
create table ta(id int identity(1,1),a varchar(50),b int)
go

create trigger tri_ta on ta for insert,update
as
if update(b) and not exists(select 1 from ta a,inserted b where b.id=a.id and charindex(','+ltrim(b.b)+',',','+a.a+',')>0)
begin
update ta set a =
case len(a.a)-len(replace(a.a,',',''))
when 4 then ltrim(b.b) + ',' + reverse(stuff(reverse(a.a),1,charindex(',',reverse(a.a)),''))
else ltrim(b.b)+isnull(','+a.a,'')
end
from ta a,inserted b where a.id=b.id
end
go

--测试:
insert into ta(b) select 1
select * from ta

update ta set b=5 where id=1
select * from ta

update ta set b=5 where id=1
select * from ta

update ta set b=4 where id=1
select * from ta

update ta set b=3 where id=1
select * from ta


update ta set b=64 where id=1
select * from ta

update ta set b=8 where id=1
select * from ta

--删除测试
drop table ta
--小F-- 2009-05-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ks_reny 的回复:]
SQL code
create trigger up_tb
on tb
for update
as
begin
if exists(select 1 from inserted where a='') -----a为空的情况
begin
update a
set a.A=(case when b.B=1 then '1'
when b.B=2 then '1,2'
else null end)
from tb a, inserted b
where a.id=b.id
end
else
begin
......…
[/Quote]

学习了 谢谢 我不怎么懂触发器
ks_reny 2009-05-16
  • 打赏
  • 举报
回复

create trigger up_tb
on tb
for update
as
begin
if exists(select 1 from inserted where a='') -----a为空的情况
begin
update a
set a.A=(case when b.B=1 then '1'
when b.B=2 then '1,2'
else null end)
from tb a, inserted b
where a.id=b.id
end
else
begin
.........和上面的类似。
end
end
--小F-- 2009-05-16
  • 打赏
  • 举报
回复
触发器怎么解决 请高手给意见啊
lg3605119 2009-05-16
  • 打赏
  • 举报
回复
触发器可以解决 ,自己组织好逻辑就行,楼主尽量自己写吧 ~~
dacsd 2009-05-16
  • 打赏
  • 举报
回复
case when.. then.. end..
JonasFeng 2009-05-16
  • 打赏
  • 举报
回复
类似这种判断都可以在触发器中完成。
ai_li7758521 2009-05-16
  • 打赏
  • 举报
回复
触发器,内部做些判断
Zoezs 2009-05-16
  • 打赏
  • 举报
回复
触发器做。
--小F-- 2009-05-16
  • 打赏
  • 举报
回复
学习
SQL77 2009-05-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 sxmonsy 的帖子:]
在字段A中保存头5回字段B的修改值.字段B只有一个数字。要是B的值在A中已经有了就不更改。
在SQL2000下操作。触发器或存储过程都行。
例子:
当A为空时:''
更改B值为1时;
更改A值为:"1"

更改B值为2时;
更改A值为:"2,1"

当A为:'1,2,3,4,5'
更改字段B为:6
A改为:'6,1,2,3,4'

更改字段B为:7
A改为:'7,6,1,2,3'

更改字段B为:1
A中有1不更改:'7,6,1,2,3'


更改字段B为:10
A更改:…
[/Quote]

触发器里一个一个判断吧!!!
加载更多回复(2)
本课程采用了漫画+动手实操+练习讲授Python编程技能。课程简介:第6章 容器类型数据6.1 序列6.1.1 序列的索引操作6.1.2 加与乘操作6.1.3 切片操作6.1.4 成员测试6.2 列表6.2.1 创建列表6.2.2 追加元素6.2.3 插入元素6.2.4 替换元素6.2.5 删除元素6.3 元组6.3.1 创建元组6.3.2 元组拆包6.4 集合6.4.1 创建集合6.4.2 修改集合6.5 字典6.5.1 创建字典6.5.2 修改字典6.5.3 访问字典视图6.6 动动手 —— 遍历字典6.7 练一练第7章 字符串7.1 字符串的表示方式7.1.1 普通字符串7.1.2 原始字符串7.1.3 长字符串7.2 字符串与数字的相互转换7.2.1 将字符串转换为数字7.2.2 将数字转换为字符串7.3 格式化字符串7.3.1 使用占位符7.3.2 格式化控制符7.4 操作字符串7.4.1 字符串查找7.4.2 字符串替换7.4.3 字符串分割7.5 动动手 —— 统计英文文章中单词出现的频率7.6 练一练第8章 函数8.1 定义函数8.2 调用函数8.2.1 使用位置参数调用函数8.2.2 使用关键字参数调用函数8.3 参数的默认值8.4 可变参数8.4.1 基于元组的可变参数( *可变参数)8.4.2 基于字典的可变参数( **可变参数)8.5 函数中变量的作用域8.6 函数类型8.6.1 理解函数类型8.6.2 过滤函数filter()8.6.3 映射函数map()8.7 lambda()函数8.8 动动手 —— 使用更多的lambda()函数8.9 练一练第9章 类与对象9.1 面向对象9.2 定义类9.3 创建对象9.4 类的成员9.4.1 实例变量9.4.2 构造方法9.4.3 实例方法9.4.4 类变量19.5 封装9.5.1 私有变量9.5.2 私有方法9.5.3 使用属性9.6 继承性9.6.1 Python中的继承9.6.2 多继承9.6.3 重写方法9.7 多态性9.7.1 继承与多态9.7.2 鸭子类型测试与多态9.8 练一练第10章 异常处理9.8 练一练10.1 第一个异常 —— 除零异常10.2 捕获异常110.2.1 try-except语句10.2.2 多个except代码块10.2.3 多重异常捕获10.2.4 try-except语句嵌套10.3 使用finally代码块释放资源10.4 自定义异常类10.5 动动手 —— 手动引发异常10.6 练一练

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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