A01(序号) A02(编号) A03(条码)
-----------------------------------------------
001 a01 C1406190008
002 a03
003 a04
...
想根据最后表的最后一个条码字段,依次往后产生条码,比如A03最后一个条码是C1406190008,那么产生的其它的条码应该往下排C1406190009,C1406190010...
5 个解决方案
#1
你的A03(条码) 是16进制的吗?如果是16进制的,先取最大的: A03(条码) ,然后加1 。。。
如果不是16进制的,就取整数位转化为int型+1 然后再强制类型转化为varchar 跟前面的字符去拼接
如果不是16进制的,就取整数位转化为int型+1 然后再强制类型转化为varchar 跟前面的字符去拼接
#2
create table test(编号 nvarchar(20))
DECLARE @Sequence varchar(4)
SET @Sequence = RIGHT(
(
SELECT MAX(编号)
FROM test
WHERE 编号 LIKE 'C'+ CONVERT(varchar(100),GETDATE(), 12) + '%'
),
4
)
IF @Sequence IS NOT NULL
BEGIN
insert test values('C'+CONVERT(varchar(100),GETDATE(),12)+ right(cast(power(10,4) as varchar)+convert(int,@Sequence)+1,4))
END
ELSE
BEGIN
insert test values('C'+CONVERT(varchar(100),GETDATE(),12)+'0001')
END
select * from test
/*
C1406190001
C1406190002
*/
#3
create table 表A
(A01 varchar(10),A02 varchar(10),A03 varchar(20))
insert into 表A
select '001','a01','C1406190008' union all
select '002','a03','' union all
select '003','a04',''
-- 更新
update a
set a.A03=left(b.x,1)+rtrim(cast(stuff(b.x,1,1,'') as bigint)+b.r)
from 表A a
inner join
(select a.A01,
(select top 1 A03 from 表A b where b.A01<a.A01) 'x',
row_number() over(order by a.A01) 'r'
from 表A a
where a.A03='') b on a.A01=b.A01
-- 结果
select * from 表A
/*
A01 A02 A03
---------- ---------- --------------------
001 a01 C1406190008
002 a03 C1406190009
003 a04 C1406190010
(3 row(s) affected)
*/
#4
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-06-19 16:11:53
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([A01] nvarchar(6),[A02] nvarchar(6),[A03] nvarchar(22))
insert [huang]
select '001','a01','C1406190008' union all
select '002','a03',null union all
select '003','a04',null
--------------生成数据--------------------------
;WITH CTE AS (
select [A01],[A02],[A03],ROW_NUMBER()OVER(ORDER BY A01)ID--CASE WHEN [A03] IS NULL THEN 'C'+CAST(CAST(SUBSTRING([A03],2,LEN([A03])) AS INT )+1 AS VARCHAR(30)) ELSE [A03] END [A03]
from [huang]
)
,CTE2 AS
(
SELECT A01,A02,CAST (A03 AS VARCHAR(31)) [A03],ID
FROM CTE WHERE ID =(SELECT MAX(ID) FROM CTE WHERE A03 IS NOT NULL )
UNION ALL
SELECT A.A01,A.A02,'C'+CAST(CAST(SUBSTRING(B.[A03],2,LEN(B.[A03])) AS INT )+1 AS VARCHAR(30)) [A03],A.ID
FROM CTE A INNER JOIN CTE2 B ON A.ID=B.ID+1
)
SELECT A01,A02,A03
FROM CTE2
----------------结果----------------------------
/*
A01 A02 A03
------ ------ -------------------------------
001 a01 C1406190008
002 a03 C1406190009
003 a04 C1406190010
*/
#5
你好!我这个A01是int自增长类型,应该是1,2,3...往下推,我把你这个套进去好像不行哟,提示影响行数为0。
#1
你的A03(条码) 是16进制的吗?如果是16进制的,先取最大的: A03(条码) ,然后加1 。。。
如果不是16进制的,就取整数位转化为int型+1 然后再强制类型转化为varchar 跟前面的字符去拼接
如果不是16进制的,就取整数位转化为int型+1 然后再强制类型转化为varchar 跟前面的字符去拼接
#2
create table test(编号 nvarchar(20))
DECLARE @Sequence varchar(4)
SET @Sequence = RIGHT(
(
SELECT MAX(编号)
FROM test
WHERE 编号 LIKE 'C'+ CONVERT(varchar(100),GETDATE(), 12) + '%'
),
4
)
IF @Sequence IS NOT NULL
BEGIN
insert test values('C'+CONVERT(varchar(100),GETDATE(),12)+ right(cast(power(10,4) as varchar)+convert(int,@Sequence)+1,4))
END
ELSE
BEGIN
insert test values('C'+CONVERT(varchar(100),GETDATE(),12)+'0001')
END
select * from test
/*
C1406190001
C1406190002
*/
#3
create table 表A
(A01 varchar(10),A02 varchar(10),A03 varchar(20))
insert into 表A
select '001','a01','C1406190008' union all
select '002','a03','' union all
select '003','a04',''
-- 更新
update a
set a.A03=left(b.x,1)+rtrim(cast(stuff(b.x,1,1,'') as bigint)+b.r)
from 表A a
inner join
(select a.A01,
(select top 1 A03 from 表A b where b.A01<a.A01) 'x',
row_number() over(order by a.A01) 'r'
from 表A a
where a.A03='') b on a.A01=b.A01
-- 结果
select * from 表A
/*
A01 A02 A03
---------- ---------- --------------------
001 a01 C1406190008
002 a03 C1406190009
003 a04 C1406190010
(3 row(s) affected)
*/
#4
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-06-19 16:11:53
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([A01] nvarchar(6),[A02] nvarchar(6),[A03] nvarchar(22))
insert [huang]
select '001','a01','C1406190008' union all
select '002','a03',null union all
select '003','a04',null
--------------生成数据--------------------------
;WITH CTE AS (
select [A01],[A02],[A03],ROW_NUMBER()OVER(ORDER BY A01)ID--CASE WHEN [A03] IS NULL THEN 'C'+CAST(CAST(SUBSTRING([A03],2,LEN([A03])) AS INT )+1 AS VARCHAR(30)) ELSE [A03] END [A03]
from [huang]
)
,CTE2 AS
(
SELECT A01,A02,CAST (A03 AS VARCHAR(31)) [A03],ID
FROM CTE WHERE ID =(SELECT MAX(ID) FROM CTE WHERE A03 IS NOT NULL )
UNION ALL
SELECT A.A01,A.A02,'C'+CAST(CAST(SUBSTRING(B.[A03],2,LEN(B.[A03])) AS INT )+1 AS VARCHAR(30)) [A03],A.ID
FROM CTE A INNER JOIN CTE2 B ON A.ID=B.ID+1
)
SELECT A01,A02,A03
FROM CTE2
----------------结果----------------------------
/*
A01 A02 A03
------ ------ -------------------------------
001 a01 C1406190008
002 a03 C1406190009
003 a04 C1406190010
*/
#5
你好!我这个A01是int自增长类型,应该是1,2,3...往下推,我把你这个套进去好像不行哟,提示影响行数为0。