这是我的过程:
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 添加新用户时它会失败,但在现有用户返回时工作正常。知道为什么在我进行插入时它没有返回任何记录吗?