---
# System prepended metadata

title: SQL 查ID
tags: [SQL]

---

# SQL 查ID 
###### tags: `SQL`
DECLARE @ID      VARCHAR(10) = UPPER('A100987638');
DECLARE @ID11    VARCHAR(11) = '';
DECLARE @FL      VARCHAR(1) ;
DECLARE @FL_IDX  INT        ;
DECLARE @CONST   VARCHAR(11)  = '19876543211';
DECLARE @CODETXT VARCHAR(100) = 'A10,B11,C12,D13,E14,F15,G16,H17,I34,J18,K19,L20,M21,N22,O35,P23,Q24,R25,S26,T27,U28,V29,X30,Y31';
DECLARE @NUM     VARCHAR(2);
DECLARE @TOT     INT = 0;
DECLARE @CNT     INT = 1;
SET @FL=SUBSTRING(@ID,1,1);
SET @FL_IDX=CHARINDEX(@FL,@CODETXT);
SET @NUM=SUBSTRING(@CODETXT,@FL_IDX+1,2);
SET @ID11=@NUM+SUBSTRING(@ID,2,9);
PRINT @ID11;
WHILE @CNT <= 11 
  BEGIN
    SET @TOT = @TOT + ( CAST(SUBSTRING(@ID11,@CNT,1) AS INT) * CAST(SUBSTRING(@CONST,@CNT,1) AS INT ));
    SET @CNT += 1;
  END
IF (@TOT%10) = 0 PRINT '合法'
   ELSE PRINT '不合法'