如何将这些循环的输出插入到我的表中?

时间:2015-09-25 08:34:34

标签: sql sql-server loops while-loop declare

Private Sub cmdApplyFilter_Click()

Dim qStringAppend As String, qString As String

qStringAppend = ""

If IsNull(Me.txtNameFilter) = False Then
qStringAppend = qStringAppend + " and (lcase(personFname) like '*" & LCase(Me.txtNameFilter) & "*' or lcase(personLName) like '*" & LCase(Me.txtNameFilter) & "*' )"

End If

qString = "SELECT tblPerson.personID, [personLName]+', '+[personFName] AS fullName, tblFamily.familyAFCID, tblAddress.addressLine1, " & _
        " tblFamily.personAFCID, refSource.sourceDescription AS personSource " & _
        " FROM refSource RIGHT JOIN (((tblPerson LEFT JOIN tblFamily ON tblPerson.personID = tblFamily.personID) " & _
        " LEFT JOIN lnkAddressPerson ON tblPerson.personID = lnkAddressPerson.personID) " & _
        " LEFT JOIN tblAddress ON lnkAddressPerson.addressID = tblAddress.addressID) ON refSource.sourceID = tblPerson.personSource " & _
        " WHERE lnkAddressPerson.addresslinkend is null "

qStringAppend = qStringAppend + " order by personLName, personFName;"
bindFormData qString, qStringAppend, "frmPerson"

Me.Repaint

End Sub

1 个答案:

答案 0 :(得分:3)

根据您之前提出的问题,我认为您希望生成多个密码。

但是你不能将它存储在一个变量中,所以请改用temp table / table变量。

<强> 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;

你也可能想要这样替换:

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