简体   繁体   中英

How do I insert the output of these loops into my table?

Declare @PASS char(16)

Declare @COUNTER int

SET @COUNTER = 0

While @COUNTER < 2

begin

    select @PASS =replace
                  (replace
                     (replace
                        (replace
                           (replace(
                              (select substring(@String, 1 + Number, 1) 
                               from master..spt_values
                               where number < datalength(@String)
                               order by NEWID()
                               for xml path('') 
                              ), '<', '<')
                           ,'>','>')
                        ,'&','&')
                     ,'"','''''')
                  ,'&apos','''')
   select @pass
   set @counter = @counter + 1

end

Based on your previous question I think you want to generate multiple passwords.

But you can't store it in one variable so use temp table/table variable instead.

Demo

DECLARE @String NVARCHAR(MAX) = 
     N'abcdefghijkmnopqrstuvwxyz' +  --lower letters
      'ABCDEFGHIJKMNOPQRSTUVWXYZ' +  --upper letters
      '0123456789'+  --number characters
      ')[:|!@$&<';

CREATE TABLE #passwords(Id INT IDENTITY(1,1), pass NVARCHAR(100));

DECLARE @i           INT = 0
       ,@pass_number INT = 20; -- max number of passwords

WHILE @i < @pass_number  
BEGIN
    SET @i += 1; 
    INSERT INTO #passwords(pass)
    Select [pass] = CAST(replace
                      (replace
                      (replace
                      (replace
                      (replace(
                      (select
                       substring(@String, 1 + Number, 1) 
                       from master..spt_values
                       where number < datalength(@String)
                       order by NEWID()
                       for xml path('') )
                        ,'&lt;', '<')
                        ,'&gt;','>')
                        ,'&amp;','&')
                        ,'"','''''')
                        ,'&apos','''') AS CHAR(16))
END
                      
SELECT *
FROM #passwords;

Also you probably want to replace like this:

,'&lt;', '<')
,'&gt;','>')
,'&amp;','&')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM