SQL 自定义函数 生成网卡地址,MES开发中经常会用到的
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | ALTER Function [dbo].[Fun_ReleaseMACadd] ( @CurrentSeqNo varchar (6) ) Returns varchar (18) ------------------------------------------------------------------------------------------------- As ------------------------------------------------------------------------------------------------- -----Created by Jock.Luo, 2024/09/05. -----Purpose: 得到新的流水号,目前是按照34进制来计算的. -----输入的参数为当前流水号,返回下一个流水号. ------------------------------------------------------------------------------------------------- Begin Declare @iNewSeqNo varchar (18) --Declare @macAddress varchar(18) Declare @OEMmacAddress varchar (18) Declare @FormatString varchar (36) Set @FormatString= '0123456789ABCDEF' set @OEMmacAddress= 'A1B2C3' ------------------------------------------------------------------------------------------------- Declare @CurrentFirstCode char (1) Declare @CurrentSecondCode char (1) Declare @CurrentThirdCode char (1) Declare @CurrentFourthCode char (1) Declare @CurrentLastModCode char (1) Declare @CurrentLastCode char (1) ------------------------------------------------------------------------------------------------- Declare @NextFirstCode char (1) Declare @NextSecondCode char (1) Declare @NextThirdCode char (1) Declare @NextFourthCode char (1) Declare @NextLastModCode char (1) Declare @NextLastCode char (1) ------------------------------------------------------------------------------------------------- Set @CurrentFirstCode= Substring (@CurrentSeqNo,1,1) Set @CurrentSecondCode= Substring (@CurrentSeqNo,2,1) Set @CurrentThirdCode= Substring (@CurrentSeqNo,3,1) Set @CurrentFourthCode= Substring (@CurrentSeqNo,4,1) Set @CurrentLastModCode= Substring (@CurrentSeqNo,5,1) Set @CurrentLastCode= Substring (@CurrentSeqNo,6,1) ------------------------------------------------------------------------------------------------- if len(@CurrentSeqNo)=6 begin If @CurrentSeqNo= 'FFFFFF' Begin Set @iNewSeqNo= '000000' set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) End ------------------------------------------------------------------------------------------------- If @CurrentSecondCode+@CurrentThirdCode+@CurrentFourthCode+@CurrentLastModCode+@CurrentLastCode= 'FFFFF' Begin Set @NextFirstCode= Substring (@FormatString,CharIndex(@CurrentFirstCode,@FormatString)+1,1) Set @iNewSeqNo=@NextFirstCode+ '00000' set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) End ------------------------------------------------------------------------------------------------- If @CurrentThirdCode+@CurrentFourthCode+@CurrentLastModCode+@CurrentLastCode= 'FFFF' Begin Set @NextSecondCode= Substring (@FormatString,CharIndex(@CurrentSecondCode,@FormatString)+1,1) Set @iNewSeqNo=@CurrentFirstCode+@NextSecondCode+ '0000' set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) End ------------------------------------------------------------------------------------------------- If @CurrentFourthCode+@CurrentLastModCode+@CurrentLastCode= 'FFF' Begin Set @NextThirdCode= Substring (@FormatString,CharIndex(@CurrentThirdCode,@FormatString)+1,1) Set @iNewSeqNo=@CurrentFirstCode+@CurrentSecondCode+@NextThirdCode+ '000' set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) End ------------------------------------------------------------------------------------------------- If @CurrentLastModCode+@CurrentLastCode= 'FF' Begin Set @NextFourthCode= Substring (@FormatString,CharIndex(@CurrentFourthCode,@FormatString)+1,1) Set @iNewSeqNo=@CurrentFirstCode+@CurrentSecondCode+@CurrentThirdCode+@NextFourthCode+ '00' set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) End ------------------------------------------------------------------------------------------------- If +@CurrentLastCode= 'F' Begin Set @NextLastModCode= Substring (@FormatString,CharIndex(@CurrentLastModCode,@FormatString)+1,1) Set @iNewSeqNo=@CurrentFirstCode+@CurrentSecondCode+@CurrentThirdCode+@CurrentFourthCode+@NextLastModCode+ '0' set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) End ------------------------------------------------------------------------------------------------- -----除上述情况之外的其它情况. Set @NextFirstCode=@CurrentFirstCode Set @NextSecondCode=@CurrentSecondCode Set @NextThirdCode=@CurrentThirdCode Set @NextFourthCode=@CurrentFourthCode set @NextLastModCode=@CurrentLastModCode Set @NextLastCode= Substring (@FormatString,CharIndex(@CurrentLastCode,@FormatString)+1,1) Set @iNewSeqNo=@CurrentFirstCode+@CurrentSecondCode+@CurrentThirdCode+@CurrentFourthCode+@CurrentLastModCode+@NextLastCode set @iNewSeqNo=@OEMmacAddress+@iNewSeqNo set @iNewSeqNo= SUBSTRING (@iNewSeqNo, 1, 2) + '-' + SUBSTRING (@iNewSeqNo, 3, 2)+ '-' + SUBSTRING (@iNewSeqNo, 5, 2) + '-' + SUBSTRING (@iNewSeqNo, 7, 2) + '-' + SUBSTRING (@iNewSeqNo, 9, 2) + '-' + SUBSTRING (@iNewSeqNo, 11, 2) Return (@iNewSeqNo) ------------------------------------------------------------------------------------------------- end Return (@iNewSeqNo) ------------------------------------------------------------------------------------------------- End |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探