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,字段名 ...
随机推荐
- BZOJ 1123 [POI2008]BLO(Tarjan算法)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1123 [题目大意] Byteotia城市有n个towns,m条双向roads. 每条r ...
- 【分块】【暴力】XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem I. Rage Minimum Query
1000w的数组,一开始都是2^31-1,然后经过5*10^7次随机位置的随机修改,问你每次的全局最小值. 有效的随机修改的期望次数很少,只有当修改到的位置恰好是当前最小值的位置时才需要扫一下更新最小 ...
- 【最大权闭合子图】BZOJ1497[NOI2006]-最大获利
[题目大意] 建立第i个通讯中转站需要的成本为Pi(1≤i≤N).另外公司调查得出了所有期望中的用户群,一共M个.关于第i个用户群的信息概括为Ai, Bi和Ci:这些用户会使用中转站Ai和中转站Bi进 ...
- dom操作 属性操作 样式操作
jQuery DOM操作 1 插入子元素 append('<img>') 插后面 被插入元素调用 appendTo('<img scr="...">') 新 ...
- 一步一步搭建springCloud
一.spring cloud简介Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- 记录Debug神经网络的方法
debugNNIntroduction to debugging neural networksThe following advice is targeted at beginners to neu ...
- session cookie的区别最全总结
作为一名WEB开发程序员,对session的理解是最基础的,但是现状是WEB程序员遍地都是,随便一划拉一大把,不过估计能把session能透彻理解的人应该不是很多,起码我之前对此是知之甚少,偶然看到的 ...
- 用windowsapi来建立一个窗口
#include <iostream> #include <Windows.h> HINSTANCE g_hInstace = ; LRESULT CALLBACK Windo ...
- rocketmq持久化方式
推荐看下RocketMQ,使用文件做持久化, 并支持分布式事务(虽然可能造成较多的写脏), 异步刷盘,内存预分配, 高可用采用了同步双写及异步复制的方式, 通信是用netty做的,基本上所有耗时的操作 ...
- edittext 手机号、邮箱输入限制
package com.example.yanlei.myapplication; import android.support.v7.app.AppCompatActivity;import a ...