-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFUNC FVE_NextCode.sql
More file actions
35 lines (32 loc) · 1.14 KB
/
FUNC FVE_NextCode.sql
File metadata and controls
35 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Alter function FVE_NextCode() returns varchar(8)
begin
--Select dbo.FVE_NextCode()
Declare @Char char,
@Code varchar(8),
@NewCode varchar(8)='',
@Qry varchar(1000),
@i int = 0,
@bool tinyint = 1;
--Set @Qry = 'Select 10'
--Set @Qry = REPLACE('Select Max(Code) From [{0}]','{0}', @tabela)
--exec (@Qry)
--Select @Code = isnull(Max(Code),'00000000') From [@ADD_MODULOS] where 1= 0
--Setar a tabela que quer que use a função; Não fiz recebendo tabela por parametro porque não consigo dar EXEC(@Qry) dentro de func.
Select @Code = isnull(Max(Code),'00000000') From [@ADD_MODULOS]
While(@i <8)
Begin
Set @Char = SUBSTRING(@Code,8-@i, 1)
Select @NewCode = CASE
When ASCII(@Char) between 48 and 56 And @bool =1 then CHAR(ASCII(@CHAR)+1)
When ASCII(@Char) = 57 And @bool =1 then 'A'
When ASCII(@Char) between 65 and 89 And @bool =1 then CHAR(ASCII(@CHAR)+1)
When ASCII(@Char) = 90 And @bool =1 then '0'
else @Char
End + @NewCode
--print ASCII(@Char)+1
IF(ASCII(@Char) != 90) Begin Set @bool = 0 end
Set @i = @i + 1
End
--Select @NewCode
Return (@NewCode)
End