当前位置: 代码迷 >> Sql Server >> SQL 某数字和字母自助组合,且无重复数字的语法,该怎么解决
  详细解决方案

SQL 某数字和字母自助组合,且无重复数字的语法,该怎么解决

热度:66   发布时间:2016-04-27 12:22:55.0
SQL 某数字和字母自助组合,且无重复数字的语法

给出某个数值,例,1,2,3,4,5


要求显示结果:

123
234
345
451
512
--


某数字和字母自助组合,且无重复数字的语法

------解决方案--------------------
SQL code
declare @M varchar(50),        @N xml        set @M='10663,10662,10661,10660'set @N=convert(xml,'<r><c>'+replace(@M,',','</c><c>')+'</c></r>')select col=T.c.value('.[1]','varchar(20)')from @N.nodes('/r/c') T(c)/*****************col--------------------10663106621066110660(4 行受影响)
------解决方案--------------------
SQL code
SET NOCOUNT ONDECLARE @Ints VARCHAR(1000)DECLARE @Table TABLE(ID INT IDENTITY(1,1),unit VARCHAR(10),choose BIT)DECLARE @Door_Int INTDECLARE @Unit VARCHAR(10)DECLARE @OutputN INT     DECLARE @OutputM INT     DECLARE @Line INTDECLARE @Rand INTDECLARE @Result VARCHAR(100) = ''DECLARE @Num INTSET @Ints = '1,2,3,4,5,A,B,C'SET @Door_Int = 1SET @OutputN = 10       --产生随机组合个数SET @OutputM = 3        --产生随机数位数SET @Num = 0WHILE @Door_Int > 0BEGIN    SET @Door_Int = CHARINDEX(',',@Ints)    IF @Door_Int = 0    BEGIN        BREAK    END    SET @Unit = LEFT(@Ints,@Door_Int - 1)    SET @Ints = RIGHT(@Ints,LEN(@Ints) - @Door_Int)        INSERT INTO @Table VALUES (@Unit,0)    ENDSELECT @Line = MAX(ID) FROM @TableSET @Door_Int = 0WHILE @Door_Int < @OutputNBEGIN    SET @Num = 0    WHILE @Num < @OutputM    BEGIN                SET @Rand = RAND() * @Line + 1                IF EXISTS(SELECT 1 FROM @Table WHERE ID = @Rand AND choose = 0)        BEGIN            SELECT @Result = @Result + unit FROM @Table WHERE ID = @Rand                        UPDATE @Table SET choose = 1 WHERE ID = @Rand                    SET @Num = @Num + 1        END            END        SET @Door_Int = @Door_Int + 1    UPDATE @Table SET choose = 0    PRINT @Result    SET @Result = ''END431314214B34523253532A1B452A52
------解决方案--------------------
探讨

SQL code

SET NOCOUNT ON
DECLARE @Ints VARCHAR(1000)
DECLARE @Table TABLE(ID INT IDENTITY(1,1),unit VARCHAR(10),choose BIT)
DECLARE @Door_Int INT
DECLARE @Unit VARCHAR(10)
DECLARE @OutputN INT
……

------解决方案--------------------
SQL code
CREATE TABLE #(ID int)INSERT INTO #SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5SELECT LTRIM(A.ID)+LTRIM(B.ID)+LTRIM(C.ID) FROM # A,# B, # CWHERE     A.id<>B.id     AND B.id<>C.id     AND A.id<>C.idORDER BY     LTRIM(A.ID)+LTRIM(B.ID)+LTRIM(C.ID)/*123124125132134135142143145152153154213214215231234235241243245251253254312314315321324325341342345351352354412413415421423425431432435451452453512513514521523524531532534541542543*/
------解决方案--------------------
探讨

SQL code

CREATE TABLE #(ID int)

INSERT INTO #
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5

SELECT LTRIM(A.ID)+LTRIM(B.ID)+LTRIM(C.ID)
FROM # A,# B, # ……
  相关解决方案