可以通过functino来解决代码use TestDb goCreate function GetTop4CharByPKID(@PKID int)returns nchar(4) as BeginDeclare @returnchar nchar(4)set @returnchar=(select case when @PKID between 1 and 9 then '000'+cast(@PKID as nchar(4))when @PKID between 10 and 99 then '00'+cast(@PKID as nchar(4))when @PKID between 100 and 999 then '0'+cast(@PKID as nchar(4))when @PKID between 999 and 9999 then '0'+cast(@PKID as nchar(4))when @PKID >9999 then '....'end )return @returncharendgoselect dbo.GetTop4CharByPKID(1) as Numberunion allselect dbo.GetTop4CharByPKID(99) as Numberunion allselect dbo.GetTop4CharByPKID(100) as Numberunion allselect dbo.GetTop4CharByPKID(599) as Numberunion allselect dbo.GetTop4CharByPKID(50229) as Number 执行结果:Number0001009901000599....