SQL SERVER 如何处理带字母的自增列--【叶子】
- --需求说明:
- /*
- id col
- ---------- ----------
- AB00001 a
- AB00002 b
- --当再插入数据的时候让id自动变成AB00003
- */
- --1.求最大值法(高并发时不适用,只是介绍个思路)
- --测试数据
- if object_id('[macotb]') is not null
- drop table [macotb]
- create table [macotb] (id varchar(7),col varchar(1))
- insert into [macotb]
- select 'AB00001','a' union all
- select 'AB00002','b'
- declare @max varchar(7)
- select @max='AB'+right('00000'+ltrim(max(replace(id,'AB','')+1)),5) from [macotb]
- insert into [macotb] select @max,'c'
- select * from [macotb]
- /*
- id col
- ------- ----
- AB00001 a
- AB00002 b
- AB00003 c
- */
- --2.利用@@identity,分步处理
- if object_id('[macotb]') is not null
- drop table [macotb]
- create table [macotb] ([no] int identity,id varchar(7),col varchar(1))
- insert into [macotb]
- select 'AB00001','a' union all
- select 'AB00002','b'
- insert into [macotb](col) select 'c'
- update [macotb]
- set id='AB'+right('00000'+ltrim([no]),5) where [no]=@@identity
- select id,col from [macotb]
- /*
- id col
- ------- ----
- AB00001 a
- AB00002 b
- AB00003 c
- */
- --3.直接添加运算列
- if object_id('[macotb]') is not null
- drop table [macotb]
- create table [macotb]
- (
- [no] int identity,
- id as ('AB'+right('00000'+ltrim([no]),5)),
- col varchar(1)
- )
- insert into [macotb](col) select 'a' union all select 'b'
- select id,col from [macotb]
- /*
- id col
- ------------ ----
- AB00001 a
- AB00002 b
- */
- insert into [macotb](col) select 'c' union all select 'd'
- select id,col from [macotb]
- /*
- id col
- ------------ ----
- AB00001 a
- AB00002 b
- AB00003 c
- AB00004 d
- */
- --叶子建议使用第三种方式!
SQL SERVER 如何处理带字母的自增列--【叶子】的更多相关文章
- SQL Server FOR XML PATH 语句的应用---列转行
经常在论坛看到高手使用了 for xml path,由于是搜索一下,记录了详细的使用方法.在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用 ...
- SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)
问: Hi, I have a table workcachedetail with 40 million rows which has 8 columns.We decided to partiti ...
- Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别
预备知识:SQLServer的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:iden ...
- Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别(转载)
预备知识:SQL Server的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:ide ...
- SQL Server事务回滚对自增键的影响
SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...
- SQL SERVER 自带系统存储过程分类
目录存储过程 用于实现 ODBC 数据字典功能,并隔离 ODBC 应用程序以使其不受基础系统表更改的影响. 变更数据捕获存储过程 用于启用.禁用.或报告变更数据捕获对象. 游标存储过程 用于实现游标变 ...
- Sql Server-使用Sql Server自带的分词功能实现字段关键词提取(分词能力很低,慎用)
“创建全文索引 启动服务 在SQL Server配置管理工具中,找到'SQL Full-text Filter Daemon Launcher'服务用本地用户启动. 创建全文目录 打开需要创建全文目录 ...
- SQL Server 创建表 添加主键 添加列常用SQL语句
--删除主键 alter table 表名 drop constraint 主键名 --添加主键 alter table 表名 add constraint 主键名 primary key(字段名1, ...
- SQL Server 创建表 添加主键 添加列常用SQL语句【转】
--删除主键alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段名 ...
随机推荐
- 【树形dp】Godfather
[POJ3107]Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7212 Accepted: 253 ...
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 8) Clustering & Dimensionality Reduction
本周主要介绍了聚类算法和特征降维方法,聚类算法包括K-means的相关概念.优化目标.聚类中心等内容:特征降维包括降维的缘由.算法描述.压缩重建等内容.coursera上面Andrew NG的Mach ...
- Idea集成svn
Idea集成svn 既然要使用svn,首先需要下载一个 svn的客户端,到这里下载对应的安装程序:http://subversion.apache.org/packages.html#windows ...
- Ubuntu下查看APT安装的软件安装路径和版本
1.查询安装路径 dpkg -L 软件名 例如:dpkg -L gedit dpkg -L gedit /. /usr /usr/bin /usr/bin/gedit /usr/share /usr/ ...
- Nginx 重定向 伪静态 rewrite index.php
参考https://www.kancloud.cn/manual/thinkphp5/177576 thinkphp入口文件同目录下添加.把下面的内容保存为.htaccess文件 <IfModu ...
- 在XC2440上实现使用U盘自动更新系统的uboot
转:http://blog.chinaunix.net/uid-22030783-id-3347599.html 此版本uboot在XC2440_uboot_V1.3版本上修改得来,是为我们的一个大客 ...
- java接入极光推送
直接提供工具类, 调用时直接调用其 sendToRegistrationId() 方法 1 import cn.jiguang.common.resp.APIConnectionException; ...
- ejs循环实例
... //index page var items=[{title:"文章1"},{title:"文章2"}]; app.get('/',function(r ...
- jquery动态添加表单数据
动态添加用户 实现代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...
- [转]Data Flow How-to Topics (SSIS)
本文转自:http://technet.microsoft.com/en-us/library/ms137612(v=sql.90).aspx This section contains proced ...