1

这是我的过程:

 ALTER PROCEDURE [dbo].[insert_user]
        @username varchar(50)
     AS
     BEGIN
        IF EXISTS(SELECT userid FROM users WHERE username = @username)
            SELECT userid FROM users WHERE username = @username
        ELSE
            BEGIN
                INSERT INTO users (username) VALUES (@username)
                SELECT userid FROM users WHERE username = @username
            END
     END

这是我的 VBA

Public userid As Integer
    Public dbconn As ADODB.Connection

Public Function Startup()
    Dim cmd As New ADODB.Command
    Dim rs As New ADODB.Recordset
    Dim param As New ADODB.Parameter

    Set dbconn = New ADODB.Connection
    dbconn.ConnectionString = "[redacted]"
    dbconn.Open dbconn.ConnectionString

    Set cmd = New ADODB.Command
    cmd.ActiveConnection = dbconn
    cmd.CommandType = adCmdStoredProc
    cmd.CommandText = "insert_user"

    Set param = cmd.CreateParameter("username", adVarChar, adParamInput, 50, Environ("UserDomain") & "dddddddd\" & Environ("Username"))
    cmd.Parameters.Append param

    rs.CursorType = adOpenStatic
    rs.CursorLocation = adUseClient
    rs.LockType = adLockOptimistic
    rs.Open cmd

    MsgBox (rs("userid"))
End Function

每当 proc 添加新用户时它会失败,但在现有用户返回时工作正常。知道为什么在我进行插入时它没有返回任何记录吗?

4

1 回答 1

1

也许这篇文章可以帮助你:http ://www.sqlteam.com/forums/topic.asp?TOPIC_ID=62936

所以,实际上,它是在你的存储过程中添加一行:

...
SET NOCOUNT ON
...
于 2012-05-18T23:36:54.227 回答