看看这个字符串的问题在哪?我是怎么也调不通了!谢谢
use pubs
declare @chvSource sysname, --数据源
@chvCol sysname, --字段名
@intLen int, --字符串的宽度
@chrRetuCode varchar(20),
@chvExec nvarchar(2000)
set @chvSource = 'sales'
set @intLen = 4
set @chvCol = 'stor_id'
Select @chvExec = 'Select @chrRetuCode =
(CASE When Exists(Select * From ['+@chvSource+'] A Where A.['+@chvCol+'] = '''+REPLICATE('0',@intLen - 1) + '1'')
Then '''+REPLICATE('0',@intLen-LEN(Convert(varchar(20),Convert(int,MIN('['+@chvCol+']')) + 1))) + Convert(varchar(20),Convert(int,MIN('['+@chvCol+']')) + 1)+'''
Else '''+REPLICATE('0',@intLen - 1) + '1''
END)
From ['+@chvSource+']
Where Not ['+@chvCol+'] IN (Select REPLICATE(''0'',@intLen-LEN(Convert(varchar(20),Convert(bigint,B.['+@chvCol+']) - 1)))
From ['+@chvSource+'] B)'
--我想最后得到这样的效果,下面的可以运行
use pubs
declare @chvSource sysname, --数据源
@chvCol sysname, --字段名
@intLen int, --字符串的宽度
@chrRetuCode varchar(20),
@chvExec nvarchar(2000)
set @chvSource = 'sales'
set @intLen = 4
set @chvCol = 'stor_id'
Select (CASE When Exists(Select * From sales A Where A.stor_id = REPLICATE('0',@intLen - 1) + '1')
Then REPLICATE('0',@intLen-LEN(Convert(varchar(20),Convert(int,MIN(stor_id)) + 1))) + Convert(varchar(20),Convert(int,MIN(stor_id)) + 1)
Else REPLICATE('0',@intLen - 1) + '1'
END)
From sales
Where Not stor_id IN (Select REPLICATE('0',@intLen-LEN(Convert(varchar(20),Convert(int,B.stor_id) - 1)))
From sales B)