1. --需求说明:
  2. /*
  3. id         col
  4. ---------- ----------
  5. AB00001    a
  6. AB00002    b
  7. --当再插入数据的时候让id自动变成AB00003
  8. */
  9. --1.求最大值法(高并发时不适用,只是介绍个思路)
  10. --测试数据
  11. if object_id('[macotb]') is not null
  12. drop table [macotb]
  13. create table [macotb] (id varchar(7),col varchar(1))
  14. insert into [macotb]
  15. select 'AB00001','a' union all
  16. select 'AB00002','b'
  17. declare @max varchar(7)
  18. select @max='AB'+right('00000'+ltrim(max(replace(id,'AB','')+1)),5) from [macotb]
  19. insert into [macotb] select @max,'c'
  20. select * from [macotb]
  21. /*
  22. id      col
  23. ------- ----
  24. AB00001 a
  25. AB00002 b
  26. AB00003 c
  27. */
  28. --2.利用@@identity,分步处理
  29. if object_id('[macotb]') is not null
  30. drop table [macotb]
  31. create table [macotb] ([no] int identity,id varchar(7),col varchar(1))
  32. insert into [macotb]
  33. select 'AB00001','a' union all
  34. select 'AB00002','b'
  35. insert into [macotb](col) select 'c'
  36. update [macotb]
  37. set id='AB'+right('00000'+ltrim([no]),5) where [no]=@@identity
  38. select id,col from [macotb]
  39. /*
  40. id      col
  41. ------- ----
  42. AB00001 a
  43. AB00002 b
  44. AB00003 c
  45. */
  46. --3.直接添加运算列
  47. if object_id('[macotb]') is not null
  48. drop table [macotb]
  49. create table [macotb]
  50. (
  51. [no] int identity,
  52. id as ('AB'+right('00000'+ltrim([no]),5)),
  53. col varchar(1)
  54. )
  55. insert into [macotb](col) select 'a' union all select 'b'
  56. select id,col from [macotb]
  57. /*
  58. id           col
  59. ------------ ----
  60. AB00001      a
  61. AB00002      b
  62. */
  63. insert into [macotb](col) select 'c' union all select 'd'
  64. select id,col from [macotb]
  65. /*
  66. id           col
  67. ------------ ----
  68. AB00001      a
  69. AB00002      b
  70. AB00003      c
  71. AB00004      d
  72. */
  73. --叶子建议使用第三种方式!

SQL SERVER 如何处理带字母的自增列--【叶子】的更多相关文章

  1. SQL Server FOR XML PATH 语句的应用---列转行

    经常在论坛看到高手使用了 for xml path,由于是搜索一下,记录了详细的使用方法.在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用 ...

  2. SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)

    问: Hi, I have a table workcachedetail with 40 million rows which has 8 columns.We decided to partiti ...

  3. Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别

    预备知识:SQLServer的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:iden ...

  4. Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别(转载)

    预备知识:SQL Server的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:ide ...

  5. SQL Server事务回滚对自增键的影响

    SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...

  6. SQL SERVER 自带系统存储过程分类

    目录存储过程 用于实现 ODBC 数据字典功能,并隔离 ODBC 应用程序以使其不受基础系统表更改的影响. 变更数据捕获存储过程 用于启用.禁用.或报告变更数据捕获对象. 游标存储过程 用于实现游标变 ...

  7. Sql Server-使用Sql Server自带的分词功能实现字段关键词提取(分词能力很低,慎用)

    “创建全文索引 启动服务 在SQL Server配置管理工具中,找到'SQL Full-text Filter Daemon Launcher'服务用本地用户启动. 创建全文目录 打开需要创建全文目录 ...

  8. SQL Server 创建表 添加主键 添加列常用SQL语句

    --删除主键 alter table 表名 drop constraint 主键名 --添加主键 alter table 表名 add constraint 主键名 primary key(字段名1, ...

  9. SQL Server 创建表 添加主键 添加列常用SQL语句【转】

    --删除主键alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段名 ...

随机推荐

  1. 【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

    一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树……但是会TLE. LCT一样无脑,但是少 ...

  2. bzoj 1236: longpo的回文

    1236: longpo的回文 题目描述 一个字符串如果从左到右和从右到左读的结果是一样的,我们称之为回文串.现在给定一个字符串,我们有三种操作: 1.     添加一个字母在任何位置(可以在首尾添加 ...

  3. fastjson生成json时Null属性不显示的解决方法

    举个例子 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonMap.put(&quo ...

  4. 深度学习的GDB调试命令和经验记录

    调试的指令很简单: cd $CAFFE_ROOT, 1. gdb ./build/tools/caffe 2. 设置运行参数 set args train --solver=xxxxsolver.pr ...

  5. Java高级架构师(一)第34节:Nginx的Http模块部分的指令

    默认长链接的数目在100个 默认长链接的超时时间,一般在75S.

  6. Create process in UNIX like system

    In UNIX, as we’ve seen, each process is identified by its process identifier, which is a unique inte ...

  7. sSkinProvider.pas

    unit sSkinProvider;{$I sDefs.inc}{.$DEFINE LOGGED} interface uses Windows, Messages, SysUtils, Class ...

  8. sqlite 增加字段语句

    ALTER TABLE `table1` ADD `AAAA` VARCHAR( 10 ) NOT NULL ; ALTER TABLE 'IPC_FGUID' ADD 'iPassageway' V ...

  9. Java 的BigDecimal

    原文:http://blog.csdn.net/diyu122222/article/details/76887382 decimal decimal(18,0) 18是定点精度,0是小数位数. de ...

  10. SecureCRT connecting VM Linux show error message: The remote system refused the connection.

    SecureCRT connecting VM Linux show error message: The remote system refused the connection.