`
逆风的香1314
  • 浏览: 1416108 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自动生成剩余编号字符串

阅读更多

原帖地址:

http://community.csdn.net/Expert/topic/3220/3220744.xml?temp=.6029474

问题描述:

insert 主表 select 'Q',0000001,0000100,100,'0000001-0000100'?
union all select 'M',0000001,0000200,200,'0000001-0000200'

insert 子表 select 'Q',0000011,0000030,20
union all select 'Q',0000032,0000050,19
union all select 'Q',0000061,0000080,20
union all select 'M',0000001,0000100,100

如何用触发器在子表中插入每条记录时,更新主表的balance字段呢?

如上想得出的结果是:
id name  strat      end     amount   balance
1   Q    0000001   0000100     41    0000001-0000010,0000031-0000031,0000051-0000060,0000081-0000100   
2   M    0000001   0000200     100   0000101-0000200

----------------------------------------------------------------------------------

--测试数据
CREATE TABLE [dbo].[主表] (
 [ID] [int] IDENTITY (1, 1) NOT NULL ,
 [name] [varchar] (50) NULL ,
 [strat] [varchar] (7) NULL ,
 [end] [varchar] (7) NULL ,
 [amount] [int] NULL ,
 [balance] [varchar] (200) NULL
)

CREATE TABLE [dbo].[子表] (
 [ID] [int] IDENTITY (1, 1) NOT NULL ,
 [name] [varchar] (50) NULL ,
 [strat] [varchar] (7) NULL ,
 [end] [varchar] (7) NULL ,
 [amount] [int] NULL
)

insert 主表 select 'Q','0000001','0000100',100,'0000001-0000100' 
union all select 'M','0000001','0000200',200,'0000001-0000200'
go

--处理的触发器
create trigger tr_process on 子表
for insert,update,delete
as
select *,[balance]=cast(null as [varchar] (200))
into #t
from 子表 a
where exists(select 1 from inserted where name=a.name)
 or exists(select 1 from deleted where name=a.name)
order by name,[strat]

declare @name varchar(10),@end int,@re varchar(200)
update #t set @re=case @name when name
  then case when @end+1<[strat] then @re+','+right(10000001+@end,7)+'-'+right(9999999+[strat],7)
   else @re+'' end else '' end
 ,@end=[end],@name=name,[balance]=@re

update a set [balance]
 =case when a.[strat]<b.[strat] then a.[strat]+'-'+right(9999999+b.[strat],7) else '' end
  +case when a.[strat]<b.[strat] then b.[balance]
   else case when b.[balance]<>'' then stuff(b.[balance],1,1,'') else '' end end 
  +case when b.[end]<a.[end]
   then case when a.[strat]<b.[strat] or b.[balance]<>''
    then ',' else '' end+right(10000001+b.[end],7)+'-'+a.[end]
   else '' end
 ,[amount]=cast(a.[end] as int)-a.[strat]-b.[amount]+1
from 主表 a,(
 select name,[strat]=min([strat]),[end]=max([end]),[balance]=max([balance]),[amount]=sum([amount])
 from #t
 group by name
)b where a.name=b.name

--处理在子表中被全部删除的数据
if exists(select 1 from deleted a where not exists(select 1 from 子表 where name=a.name))
 update a set amount=cast(a.[end] as int)-a.[strat]+1
  ,[balance]=a.[strat]+'-'+a.[end]
 from 主表 a,(
  select distinct name from deleted a
  where not exists(select 1 from 子表 where name=a.name)
 )b where a.name=b.name
go

--插入子表数据
insert 子表 select 'Q','0000011','0000030',20
union all select 'Q','0000032','0000050',19
union all select 'Q','0000061','0000080',20
union all select 'M','0000051','0000100',50

--更新
update 子表 set name='M'
where ID=1

delete from 子表 where id in(1,3,4)
go

--显示处理结果
select * from 主表
select * from 子表
go

drop table 主表,子表

/*--测试结果

ID  name   strat   end     amount   balance                 
--- ------ ------- ------- -------- -------------------------------
1   Q      0000001 0000100 81       0000001-0000031,0000051-0000100
2   M      0000001 0000200 200      0000001-0000200

(所影响的行数为 2 行)

ID          name   strat   end     amount     
----------- ------ ------- ------- ----------
2           Q      0000032 0000050 19

(所影响的行数为 1 行)
--*/

分享到:
评论

相关推荐

    python生成随机的测验试卷文件.docx

    【Python生成随机测验试卷...综上所述,Python生成随机测验试卷文件涉及到的数据结构、随机数生成、文件操作、字符串处理、错误处理以及模块化编程等多个核心知识点,是一个很好的实践项目,有助于提升Python编程能力。

    delphi 开发经验技巧宝典源码

    0127 将错误编号转换为错误信息字符串 83 0128 使用ExceptAddr函数获得异常被抛出的地址 83 0129 格式化异常处理信息 84 4.8 图形图像相关函数 85 0130 将TColor类型的颜色值转换为RGB值 85 0131 使用...

    delphi 开发经验技巧宝典源码06

    0127 将错误编号转换为错误信息字符串 83 0128 使用ExceptAddr函数获得异常被抛出的地址 83 0129 格式化异常处理信息 84 4.8 图形图像相关函数 85 0130 将TColor类型的颜色值转换为RGB值 85 0131 使用...

    python爬取小说-27-while循环创建有规律的列表.ev4.rar

    这里,`url_format`是章节URL的模板,`{:03d}`是格式化字符串,确保章节编号始终是三位数,即使章节编号是1或10。 除了生成列表,`while`循环还可以与`break`和`continue`语句结合使用,以实现更复杂的控制流程。`...

    _leetcode-python.pdf

    - Longest Common Prefix: 找出一个字符串数组中所有字符串的最长公共前缀。 - 3Sum / 3Sum Closest / 4Sum: 这些题目都涉及到在数组中寻找具有特定和的数字组合,这通常需要用到双指针技术。 - Remove Nth Node ...

    EXCEL集成工具箱V6.0

    在窗体中双击文本字符串尾即可实现自动选定文本并自动复制功能。本工具较同类转换工具准确率达100%。 【背景颜色】 提供可视化的可选择着色方式的背景着色功能,默认启用智能着色。 【文件批量改名】 功能完善的...

    2021-2022计算机二级等级考试试题及答案No.14476.docx

    - 自动编号类型的字段数据是系统自动生成的,用户不能直接编辑。 - 是/否型数据可以用于索引。 - 货币型数据的小数部分超出2位时,Access会自动四舍五入。 - 默认情况下,文本类型字段的大小是50,而不是10。 ...

    EXCEL集成工具箱V8.0完整增强版(精简)

    在窗体中双击文本字符串尾即可实现自动选定文本并自动复制功能。本工具较同类转换工具准确率达100%。 【背景颜色】 提供可视化的可选择着色方式的背景着色功能,默认启用智能着色。 【文件批量改名】 功能完善的...

    2021-2022计算机二级等级考试试题及答案No.11022.docx

    这通常是用来实现自动编号的功能,例如在用户注册时自动生成唯一的用户ID。 ### 3. 满二叉树的叶子节点数量计算 - **满二叉树**:深度为7的满二叉树具有特定的结构,每个父节点都有两个子节点。在这种情况下,叶子...

    2021-2022计算机二级等级考试试题及答案No.16453.docx

    `&lt;ol&gt;`标签定义了一个有序列表,其中的每个项目都会自动编号。 ### 21. **Switch语句的适用范围** - **适用数据类型**: - 在C语言中,`switch`语句的表达式只能是整数类型(如int、short、char或byte),不能...

    redis的学习笔记 redis.pdf

    此外,Key可以设置过期时间,使用expire命令设置过期时间后,Redis会自动删除过期的Key,ttl命令则可以查看Key的剩余存活时间。 Redis支持的五大数据类型包括String、List、Set、Sorted Set和Hash。在本次学习笔记...

    C#编程经验技巧宝典

    83 &lt;br&gt;0125 按要求生成指定位数编号 83 &lt;br&gt;0126 确定两字符串是否相等 84 &lt;br&gt;0127 判断两字符串中指定子串是否相等 84 &lt;br&gt;0128 判断字符串是否为日期格式 85 &lt;br&gt;0129 清除字符串中指定...

    2021-2022计算机二级等级考试试题及答案No.18076.docx

    9. **自动编号**数据类型可以用于为每个新记录自动生成唯一的数字。这在需要唯一标识符的情况下非常有用。正确选项是:C. 自动编号。 ### Python字符串方法 10. `str.isnumeric()`方法用于判断字符串是否全部由数字...

    2021-2022计算机二级等级考试试题及答案No.19303.docx

    在Access数据库中,对于**自动编号**类型的字段,用户通常无法直接编辑其值,因为这些值是由数据库自动生成的。另外,货币型字段可以支持小数部分超过两位的输入,但系统会自动进行四舍五入处理。 ### 9. 微型...

    2021-2022计算机二级等级考试试题及答案No.13378.docx

    题目中的示例通过比较两个字符串并根据结果进行不同的操作,最终输出的结果为`ABCD`,这表明在非精确模式下,当两个字符串的前缀相同时,可以将剩余部分进行拼接。 #### 知识点5:Swing GUI组件 - **知识点概述**:...

    idea快捷键

    - **功能**:自动生成getter、setter、构造函数等常用代码。 - **应用场景**:快速编写标准代码模板。 53. **Ctrl+E 或者 Alt+Shift+C: 最近更改的代码** - **功能**:显示最近修改过的文件。 - **应用场景**:...

Global site tag (gtag.js) - Google Analytics