原表
列1 列2 列3 列4 列5
A 35 30 2 N
B 51 17 3 N
C 14 14 1 M
需要的效果
列1 列2 列3 列4 列5 列6 列7 列8
A 35 30 2 N 30 null 191030001
A 35 30 2 N 5 null 191030002
B 51 17 3 N 17 null 191030003
B 51 17 3 N 17 null 191030004
B 51 17 3 N 17 null 191030005
C 14 14 1 M 14 191030005 191030006
DECLARE @t TABLE(T VARCHAR(1), A INT,B INT,C INT,D INT)
INSERT @t (T,A,B) VALUES('A',100,20),('B', 50,16);
UPDATE @t SET D=A;
WITH t AS (SELECT T, A, B, CASE WHEN D>B THEN B ELSE D END C,D-CASE WHEN D>B THEN B ELSE D END D,CEILING(A/B) E FROM @t
UNION ALL SELECT t.T, t.A,t.B, CASE WHEN D>B THEN B ELSE D END C,D-CASE WHEN D>B THEN B ELSE D END D,E FROM t WHERE t.D>0)
SELECT t.T, t.A, t.B, t.C, t.D, t.E FROM t ORDER BY T,D DESC
至于9位不重復的自己根據規則去處理即可
uj5u.com熱心網友回復:
我需要把結果保存到表變數或臨時表里,用WITH AS怎么實作?
uj5u.com熱心網友回復:
最后一句改成SELECT t.T, t.A, t.B, t.C, t.D, t.E INTO #temp FROM t ORDER BY T,D DESC就保存到臨時表里了
uj5u.com熱心網友回復:
不重復我可以做到,但全年不重復怎么實作?如果一天生成 一次可以是當年日期,可是一天多次怎么辦?
uj5u.com熱心網友回復:
不重復我可以做到,但全年不重復怎么實作?如果一天生成 一次可以是當年日期,可是一天多次怎么辦?
可以考慮先取出最大值,再去生成累加值,在生成的時候把表鎖住,防止他人同時更新
uj5u.com熱心網友回復:
不重復我可以做到,但全年不重復怎么實作?如果一天生成 一次可以是當年日期,可是一天多次怎么辦?
可以考慮先取出最大值,再去生成累加值,在生成的時候把表鎖住,防止他人同時更新
代碼怎么寫?
uj5u.com熱心網友回復:
用事務來執行,默認就是鎖定的,如果是存盤過來可以顯式申明事務
begin tran……
DECLARE @t TABLE(T VARCHAR(1), A INT,B INT,C INT,D INT)
INSERT @t (T,A,B) VALUES('A',100,20),('B', 50,16);
UPDATE @t SET D=A;
WITH t AS (SELECT T, A, B, CASE WHEN D>B THEN B ELSE D END C,D-CASE WHEN D>B THEN B ELSE D END D,CEILING(A/B) E FROM @t
UNION ALL SELECT t.T, t.A,t.B, CASE WHEN D>B THEN B ELSE D END C,D-CASE WHEN D>B THEN B ELSE D END D,E FROM t WHERE t.D>0)
SELECT t.T, t.A, t.B, t.C, t.D, t.E, NULL AS Rnd INTO #temp FROM t ORDER BY T,D DESC
DECLARE @max INT=100000000;--這里可以用SQL獲取最大值
UPDATE #temp SET @max=@max+1, Rnd=@max
SELECT * FROM #temp
uj5u.com熱心網友回復:
沒看懂@HELLOWORLD
uj5u.com熱心網友回復:
declare @t table(a varchar(10),b int,c int,e varchar(10))
insert into @t
select 'A' as a,35 as b,30 as c,'N' as e
union all
select 'B',51,17,'N'
union all
select 'C',14,14,'M'
declare @i int,@r int
select @i=68,@r = rand()*200
while @i<91
begin
insert into @t
select char(@i),@r,rand()*@r,(case when rand()*2>1 then 'M' else 'N' end)
select @i=@i+1,@r = rand()*200
end
--select * from @t
select t1.*,(case when b>=c*(number+1) then c else b-c*number end) as f
,(case when e='n' then null else row_number() over(partition by e order by a,number) end) as g
,row_number() over(order by a,number) as h
from (
select a,b,c,(b-1)/c+1 as d,e
from @t
) t1
left join master..spt_values v on v.type='p' and v.number<t1.d
declare @t table(a varchar(10),b int,c int,e varchar(10))
insert into @t
select 'A' as a,35 as b,30 as c,'N' as e
union all
select 'B',51,17,'N'
union all
select 'C',14,14,'M'
declare @i int,@r int
select @i=68,@r = rand()*200
while @i<91
begin
insert into @t
select char(@i),@r,rand()*@r,(case when rand()*2>1 then 'M' else 'N' end)
select @i=@i+1,@r = rand()*200
end
--select * from @t
select t1.*,(case when b>=c*(number+1) then c else b-c*number end) as f
,(case when e='n' then null else row_number() over(partition by e order by a,number) end) as g
,row_number() over(order by a,number) as h
from (
select a,b,c,(b-1)/c+1 as d,e
from @t
) t1
left join master..spt_values v on v.type='p' and v.number<t1.d
--創建一個計數器的表,按年流水,最小值100000000
CREATE TABLE A
(
y VARCHAR(4),
v BIGINT
)
GO
--創建存盤程序按年取數
create PROCEDURE sp_getvalue(@y VARCHAR(4),@cnt int)
as
BEGIN
IF @cnt<=0
return
if not exists(select 1 from A where y=@y)
begin
insert into A values(@y,100000000)
end
DECLARE @rst TABLE(V BIGINT)
UPDATE A SET v=v+@cnt OUTPUT Deleted.v INTO @rst WHERE y=@y
;
with ct
as
(
select 1 as id,V+1 As V from @rst
union all
select ID+1,V+1 from ct where id<@cnt
)
SELECT * from ct
end
--你的拆分
--臨時使用
create table #T
(
id int,
val bigint
)
insert into #T(id,val)
EXEC sp_getvalue '2019',拆分的紀錄數
--按紀錄號關聯更新
drop table #T
uj5u.com熱心網友回復:
--創建一個計數器的表,按年流水,最小值100000000
CREATE TABLE A
(
y VARCHAR(4),
v BIGINT
)
GO
--創建存盤程序按年取數
create PROCEDURE sp_getvalue(@y VARCHAR(4),@cnt int)
as
BEGIN
IF @cnt<=0
return
if not exists(select 1 from A where y=@y)
begin
insert into A values(@y,100000000)
end
DECLARE @rst TABLE(V BIGINT)
UPDATE A SET v=v+@cnt OUTPUT Deleted.v INTO @rst WHERE y=@y
;
with ct
as
(
select 1 as id,V+1 As V from @rst
union all
select ID+1,V+1 from ct where id<@cnt
)
SELECT * from ct
end
--你的拆分
--臨時使用
create table #T
(
id int,
val bigint
)
insert into #T(id,val)
EXEC sp_getvalue '2019',拆分的紀錄數
--按紀錄號關聯更新
drop table #T
執行的時候提示,從資料型nvarchar 轉換int時出錯
uj5u.com熱心網友回復:
拆分的紀錄數,這個啊,你換掉,你拆分出來 有多少 記錄,這里就填多少
EXEC sp_getvalue '2019',10
uj5u.com熱心網友回復:
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
SELECT CONVERT(varchar(10),GETDATE(),112)+RIGHT(1000000000+NEXT VALUE FOR SEQ_ID,9)
uj5u.com熱心網友回復:
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
CREATE TABLE [dbo].[SysSequenceSub](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[DateStr] [varchar](50) NOT NULL,
[CurrentValue] [bigint] NOT NULL,
[Creator] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequenceSub] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC,
[DateStr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[SysSequence] Script Date: 2019-11-15 15:22:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysSequence](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[ChineseName] [varchar](50) NULL,
[Prefix] [varchar](5) NOT NULL,
[DateLength] [int] NOT NULL,
[Width] [int] NOT NULL,
[CurrentValue] [varchar](50) NOT NULL,
[CreateDate] [datetime] NULL,
[Creator] [nvarchar](50) NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequence] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[up_Sys_GetSequenceNO_Multi]
@AccId smallint,
@SequenceName varchar(50),
@count int,
@rtn varchar(50) output,
@Width int output,
@Prefix varchar(50)=null,
@date datetime=null
AS
set nocount on;
--Declare @Prefix varchar(5)
Declare @DateLength int
--Declare @Width int
Declare @NowDate varchar(8),@CheckStr varchar(1),@CurrentValue bigint
declare @Mark varchar(50)='' --標記是否外部前綴
if @Prefix<>'' set @Mark=@Prefix
select @Prefix = case when isnull(@Prefix,'')<>'' then @Prefix else Prefix end,@DateLength = DateLength,@Width = Width,@CheckStr='Y'
from dbo.SysSequence
where SequenceName = @SequenceName and AccId=@AccId
set @rtn=''
if isnull(@CheckStr,'')='' return;
if @date is null set @date=getdate()
Set @NowDate = left(Convert(char(8),@date,12),@DateLength)
merge dbo.SysSequenceSub tg
using (select AccId=@AccId,SequenceName=@SequenceName,DateStr=@Mark+@NowDate,CurrentValue=https://bbs.csdn.net/topics/@count,CreateDate=getdate(),IsDelete=0) su
on tg.AccId=su.AccId and tg.SequenceName=su.SequenceName and tg.DateStr=su.DateStr
when matched then
update set tg.CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
,@CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
when not matched then
insert (AccId,SequenceName,DateStr,CurrentValue,CreateDate,IsDelete)
values(su.AccId,su.SequenceName,su.DateStr,su.CurrentValue,su.CreateDate,su.IsDelete)
;
set @rtn= @Prefix+@NowDate+ right(Replicate('0', @Width)+cast(isnull(@CurrentValue,@count) as varchar(20)), @Width)
uj5u.com熱心網友回復:
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
CREATE TABLE [dbo].[SysSequenceSub](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[DateStr] [varchar](50) NOT NULL,
[CurrentValue] [bigint] NOT NULL,
[Creator] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequenceSub] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC,
[DateStr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[SysSequence] Script Date: 2019-11-15 15:22:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysSequence](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[ChineseName] [varchar](50) NULL,
[Prefix] [varchar](5) NOT NULL,
[DateLength] [int] NOT NULL,
[Width] [int] NOT NULL,
[CurrentValue] [varchar](50) NOT NULL,
[CreateDate] [datetime] NULL,
[Creator] [nvarchar](50) NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequence] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[up_Sys_GetSequenceNO_Multi]
@AccId smallint,
@SequenceName varchar(50),
@count int,
@rtn varchar(50) output,
@Width int output,
@Prefix varchar(50)=null,
@date datetime=null
AS
set nocount on;
--Declare @Prefix varchar(5)
Declare @DateLength int
--Declare @Width int
Declare @NowDate varchar(8),@CheckStr varchar(1),@CurrentValue bigint
declare @Mark varchar(50)='' --標記是否外部前綴
if @Prefix<>'' set @Mark=@Prefix
select @Prefix = case when isnull(@Prefix,'')<>'' then @Prefix else Prefix end,@DateLength = DateLength,@Width = Width,@CheckStr='Y'
from dbo.SysSequence
where SequenceName = @SequenceName and AccId=@AccId
set @rtn=''
if isnull(@CheckStr,'')='' return;
if @date is null set @date=getdate()
Set @NowDate = left(Convert(char(8),@date,12),@DateLength)
merge dbo.SysSequenceSub tg
using (select AccId=@AccId,SequenceName=@SequenceName,DateStr=@Mark+@NowDate,CurrentValue=https://bbs.csdn.net/topics/@count,CreateDate=getdate(),IsDelete=0) su
on tg.AccId=su.AccId and tg.SequenceName=su.SequenceName and tg.DateStr=su.DateStr
when matched then
update set tg.CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
,@CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
when not matched then
insert (AccId,SequenceName,DateStr,CurrentValue,CreateDate,IsDelete)
values(su.AccId,su.SequenceName,su.DateStr,su.CurrentValue,su.CreateDate,su.IsDelete)
;
set @rtn= @Prefix+@NowDate+ right(Replicate('0', @Width)+cast(isnull(@CurrentValue,@count) as varchar(20)), @Width)
運行之后顯示全是空值
uj5u.com熱心網友回復:
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
CREATE TABLE [dbo].[SysSequenceSub](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[DateStr] [varchar](50) NOT NULL,
[CurrentValue] [bigint] NOT NULL,
[Creator] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequenceSub] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC,
[DateStr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[SysSequence] Script Date: 2019-11-15 15:22:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysSequence](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[ChineseName] [varchar](50) NULL,
[Prefix] [varchar](5) NOT NULL,
[DateLength] [int] NOT NULL,
[Width] [int] NOT NULL,
[CurrentValue] [varchar](50) NOT NULL,
[CreateDate] [datetime] NULL,
[Creator] [nvarchar](50) NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequence] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[up_Sys_GetSequenceNO_Multi]
@AccId smallint,
@SequenceName varchar(50),
@count int,
@rtn varchar(50) output,
@Width int output,
@Prefix varchar(50)=null,
@date datetime=null
AS
set nocount on;
--Declare @Prefix varchar(5)
Declare @DateLength int
--Declare @Width int
Declare @NowDate varchar(8),@CheckStr varchar(1),@CurrentValue bigint
declare @Mark varchar(50)='' --標記是否外部前綴
if @Prefix<>'' set @Mark=@Prefix
select @Prefix = case when isnull(@Prefix,'')<>'' then @Prefix else Prefix end,@DateLength = DateLength,@Width = Width,@CheckStr='Y'
from dbo.SysSequence
where SequenceName = @SequenceName and AccId=@AccId
set @rtn=''
if isnull(@CheckStr,'')='' return;
if @date is null set @date=getdate()
Set @NowDate = left(Convert(char(8),@date,12),@DateLength)
merge dbo.SysSequenceSub tg
using (select AccId=@AccId,SequenceName=@SequenceName,DateStr=@Mark+@NowDate,CurrentValue=https://bbs.csdn.net/topics/@count,CreateDate=getdate(),IsDelete=0) su
on tg.AccId=su.AccId and tg.SequenceName=su.SequenceName and tg.DateStr=su.DateStr
when matched then
update set tg.CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
,@CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
when not matched then
insert (AccId,SequenceName,DateStr,CurrentValue,CreateDate,IsDelete)
values(su.AccId,su.SequenceName,su.DateStr,su.CurrentValue,su.CreateDate,su.IsDelete)
;
set @rtn= @Prefix+@NowDate+ right(Replicate('0', @Width)+cast(isnull(@CurrentValue,@count) as varchar(20)), @Width)
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
CREATE TABLE [dbo].[SysSequenceSub](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[DateStr] [varchar](50) NOT NULL,
[CurrentValue] [bigint] NOT NULL,
[Creator] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequenceSub] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC,
[DateStr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[SysSequence] Script Date: 2019-11-15 15:22:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysSequence](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[ChineseName] [varchar](50) NULL,
[Prefix] [varchar](5) NOT NULL,
[DateLength] [int] NOT NULL,
[Width] [int] NOT NULL,
[CurrentValue] [varchar](50) NOT NULL,
[CreateDate] [datetime] NULL,
[Creator] [nvarchar](50) NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequence] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[up_Sys_GetSequenceNO_Multi]
@AccId smallint,
@SequenceName varchar(50),
@count int,
@rtn varchar(50) output,
@Width int output,
@Prefix varchar(50)=null,
@date datetime=null
AS
set nocount on;
--Declare @Prefix varchar(5)
Declare @DateLength int
--Declare @Width int
Declare @NowDate varchar(8),@CheckStr varchar(1),@CurrentValue bigint
declare @Mark varchar(50)='' --標記是否外部前綴
if @Prefix<>'' set @Mark=@Prefix
select @Prefix = case when isnull(@Prefix,'')<>'' then @Prefix else Prefix end,@DateLength = DateLength,@Width = Width,@CheckStr='Y'
from dbo.SysSequence
where SequenceName = @SequenceName and AccId=@AccId
set @rtn=''
if isnull(@CheckStr,'')='' return;
if @date is null set @date=getdate()
Set @NowDate = left(Convert(char(8),@date,12),@DateLength)
merge dbo.SysSequenceSub tg
using (select AccId=@AccId,SequenceName=@SequenceName,DateStr=@Mark+@NowDate,CurrentValue=https://bbs.csdn.net/topics/@count,CreateDate=getdate(),IsDelete=0) su
on tg.AccId=su.AccId and tg.SequenceName=su.SequenceName and tg.DateStr=su.DateStr
when matched then
update set tg.CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
,@CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
when not matched then
insert (AccId,SequenceName,DateStr,CurrentValue,CreateDate,IsDelete)
values(su.AccId,su.SequenceName,su.DateStr,su.CurrentValue,su.CreateDate,su.IsDelete)
;
set @rtn= @Prefix+@NowDate+ right(Replicate('0', @Width)+cast(isnull(@CurrentValue,@count) as varchar(20)), @Width)
運行之后顯示全是空值
把運行的SQL發出來
運行存盤程序結果是NULL,然后我查看代碼中的兩個表,也都是空表
uj5u.com熱心網友回復:
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
CREATE TABLE [dbo].[SysSequenceSub](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[DateStr] [varchar](50) NOT NULL,
[CurrentValue] [bigint] NOT NULL,
[Creator] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequenceSub] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC,
[DateStr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[SysSequence] Script Date: 2019-11-15 15:22:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysSequence](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[ChineseName] [varchar](50) NULL,
[Prefix] [varchar](5) NOT NULL,
[DateLength] [int] NOT NULL,
[Width] [int] NOT NULL,
[CurrentValue] [varchar](50) NOT NULL,
[CreateDate] [datetime] NULL,
[Creator] [nvarchar](50) NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequence] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[up_Sys_GetSequenceNO_Multi]
@AccId smallint,
@SequenceName varchar(50),
@count int,
@rtn varchar(50) output,
@Width int output,
@Prefix varchar(50)=null,
@date datetime=null
AS
set nocount on;
--Declare @Prefix varchar(5)
Declare @DateLength int
--Declare @Width int
Declare @NowDate varchar(8),@CheckStr varchar(1),@CurrentValue bigint
declare @Mark varchar(50)='' --標記是否外部前綴
if @Prefix<>'' set @Mark=@Prefix
select @Prefix = case when isnull(@Prefix,'')<>'' then @Prefix else Prefix end,@DateLength = DateLength,@Width = Width,@CheckStr='Y'
from dbo.SysSequence
where SequenceName = @SequenceName and AccId=@AccId
set @rtn=''
if isnull(@CheckStr,'')='' return;
if @date is null set @date=getdate()
Set @NowDate = left(Convert(char(8),@date,12),@DateLength)
merge dbo.SysSequenceSub tg
using (select AccId=@AccId,SequenceName=@SequenceName,DateStr=@Mark+@NowDate,CurrentValue=https://bbs.csdn.net/topics/@count,CreateDate=getdate(),IsDelete=0) su
on tg.AccId=su.AccId and tg.SequenceName=su.SequenceName and tg.DateStr=su.DateStr
when matched then
update set tg.CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
,@CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
when not matched then
insert (AccId,SequenceName,DateStr,CurrentValue,CreateDate,IsDelete)
values(su.AccId,su.SequenceName,su.DateStr,su.CurrentValue,su.CreateDate,su.IsDelete)
;
set @rtn= @Prefix+@NowDate+ right(Replicate('0', @Width)+cast(isnull(@CurrentValue,@count) as varchar(20)), @Width)
運行之后顯示全是空值
把運行的SQL發出來
運行存盤程序結果是NULL,然后我查看代碼中的兩個表,也都是空表
在上面第一點,要在表中SysSequence配置記錄
insert into [dbo].[SysSequence]([AccId],[SequenceName],[ChineseName],[Prefix],[DateLength],[Width],[CurrentValue],[CreateDate],[Creator],[Updater],[UpdateDate]
,[IsDelete],[CreatorCode],[UpdaterCode],[OperDept],[RoCode])
select [AccId]=1,[SequenceName]='test',[ChineseName]='測驗',[Prefix]='T',[DateLength]=6,[Width]=4,[CurrentValue]='',[CreateDate]=GETDATE(),[Creator]='',[Updater]='',[UpdateDate]=GETDATE()
,[IsDelete]=0,[CreatorCode]='',[UpdaterCode]='',[OperDept]='',[RoCode]=''
if object_id('tempdb..#list') is not null drop table #list
select 列1='A',列2=35,列3=30,列4=2,列5='N' into #list union all
select 列1='B',列2=51,列3=17,列4=3,列5='N' union all
select 列1='C',列2=14,列3=14,列4=1,列5='M'
;
with list as(
select *,mark=cast(列2/列3 as int)+iif(列2%列3>0,1,0),mark2=列2%列3
from #list
)
select 列1,a.列2,a.列3,a.列4,a.列5
,列6=case when b.number+1<a.mark then 列3
when b.number+1=a.mark and mark2>0 then mark2
else 列3
end
,[列7]=case when 列5='N' then null
else '19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)-1) as varchar(4)),4)
end
,[列8]='19103'+right('0000'+cast( (ROW_NUMBER()over(order by 列1,a.mark desc)) as varchar(4)),4)
from list a
inner join master.dbo.spt_values b on b.number+1<=a.mark
where b.type='P'
ORDER BY 列1,a.mark desc
/*
列1 列2 列3 列4 列5 列6 列7 列8
---- ----------- ----------- ----------- ---- ----------- ------------- -------------
A 35 30 2 N 30 NULL 191030001
A 35 30 2 N 5 NULL 191030002
B 51 17 3 N 17 NULL 191030003
B 51 17 3 N 17 NULL 191030004
B 51 17 3 N 17 NULL 191030005
C 14 14 1 M 14 191030005 191030006
*/
CREATE TABLE [dbo].[SysSequenceSub](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[DateStr] [varchar](50) NOT NULL,
[CurrentValue] [bigint] NOT NULL,
[Creator] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequenceSub] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC,
[DateStr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[SysSequence] Script Date: 2019-11-15 15:22:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysSequence](
[AccId] [smallint] NOT NULL,
[SequenceName] [varchar](50) NOT NULL,
[ChineseName] [varchar](50) NULL,
[Prefix] [varchar](5) NOT NULL,
[DateLength] [int] NOT NULL,
[Width] [int] NOT NULL,
[CurrentValue] [varchar](50) NOT NULL,
[CreateDate] [datetime] NULL,
[Creator] [nvarchar](50) NULL,
[Updater] [nvarchar](50) NULL,
[UpdateDate] [datetime] NULL,
[IsDelete] [tinyint] NOT NULL,
[CreatorCode] [nvarchar](50) NULL,
[UpdaterCode] [nvarchar](50) NULL,
[OperDept] [nvarchar](50) NULL,
[RoCode] [varchar](50) NULL,
CONSTRAINT [PK_SysSequence] PRIMARY KEY CLUSTERED
(
[AccId] ASC,
[SequenceName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[up_Sys_GetSequenceNO_Multi]
@AccId smallint,
@SequenceName varchar(50),
@count int,
@rtn varchar(50) output,
@Width int output,
@Prefix varchar(50)=null,
@date datetime=null
AS
set nocount on;
--Declare @Prefix varchar(5)
Declare @DateLength int
--Declare @Width int
Declare @NowDate varchar(8),@CheckStr varchar(1),@CurrentValue bigint
declare @Mark varchar(50)='' --標記是否外部前綴
if @Prefix<>'' set @Mark=@Prefix
select @Prefix = case when isnull(@Prefix,'')<>'' then @Prefix else Prefix end,@DateLength = DateLength,@Width = Width,@CheckStr='Y'
from dbo.SysSequence
where SequenceName = @SequenceName and AccId=@AccId
set @rtn=''
if isnull(@CheckStr,'')='' return;
if @date is null set @date=getdate()
Set @NowDate = left(Convert(char(8),@date,12),@DateLength)
merge dbo.SysSequenceSub tg
using (select AccId=@AccId,SequenceName=@SequenceName,DateStr=@Mark+@NowDate,CurrentValue=https://bbs.csdn.net/topics/@count,CreateDate=getdate(),IsDelete=0) su
on tg.AccId=su.AccId and tg.SequenceName=su.SequenceName and tg.DateStr=su.DateStr
when matched then
update set tg.CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
,@CurrentValue=https://bbs.csdn.net/topics/tg.CurrentValue+su.CurrentValue
when not matched then
insert (AccId,SequenceName,DateStr,CurrentValue,CreateDate,IsDelete)
values(su.AccId,su.SequenceName,su.DateStr,su.CurrentValue,su.CreateDate,su.IsDelete)
;
set @rtn= @Prefix+@NowDate+ right(Replicate('0', @Width)+cast(isnull(@CurrentValue,@count) as varchar(20)), @Width)
運行之后顯示全是空值
把運行的SQL發出來
運行存盤程序結果是NULL,然后我查看代碼中的兩個表,也都是空表
在上面第一點,要在表中SysSequence配置記錄
insert into [dbo].[SysSequence]([AccId],[SequenceName],[ChineseName],[Prefix],[DateLength],[Width],[CurrentValue],[CreateDate],[Creator],[Updater],[UpdateDate]
,[IsDelete],[CreatorCode],[UpdaterCode],[OperDept],[RoCode])
select [AccId]=1,[SequenceName]='test',[ChineseName]='測驗',[Prefix]='T',[DateLength]=6,[Width]=4,[CurrentValue]='',[CreateDate]=GETDATE(),[Creator]='',[Updater]='',[UpdateDate]=GETDATE()
,[IsDelete]=0,[CreatorCode]='',[UpdaterCode]='',[OperDept]='',[RoCode]=''
**桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......
我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......
關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......