alexbrainman / odbc

odbc driver written in go
BSD 3-Clause "New" or "Revised" License
352 stars 140 forks source link

got a problem when doing the stored procedure to sql server #85

Closed proudlily closed 7 years ago

proudlily commented 7 years ago

i have connected the sql server seccessful,but when i tired to save the stored procedure ,i got failed

my code is below:

_, err := s.DBGT.Db.Exec("CALL GO_EfficacyAccounts(?,?,?,?,?,?)", a, e, f, b, c, d)
    if err != nil {
        fmt.Println("exec err:", err)
        return
    }
}

and the err is below:

exec err: SQLExecute: {42000} [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]'@P1' 附近有语法错误。
{42000} [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]无法预定义语句。

the sql server code is :

CREATE PROC [dbo].[GO_EfficacyAccounts]
       @dwUserID INT,                            
      @strClientIP NVARCHAR(15),                  
      @strMachineID NVARCHAR(32),                 
      @dwVersion INT,                           
     @strLogFrom INT,                           
     @dwIsDelVip INT                           
 WITH ENCRYPTION 
  AS
SET NOCOUNT ON

  BEGIN

     IF @RegisterMachine='wx'
     begin
         UPDATE QPAccountsDB.dbo.AccountsInfo set RegisterMachine=@strMachineID where UserID=@UserID
    end

      UPDATE QPAccountsDB.dbo.AccountsInfo SET GameLogonTimes=GameLogonTimes+1,LastLogonDate=GETDATE(), LastLogonIP=@strClientIP,
         LastLogonMachine=@strMachineID,LockMobileKindID=@strLogFrom,SerialNumber=@dwVersion WHERE UserID=@UserID                                             

      IF @dwIsDelVip>0
      BEGIN
         UPDATE AccountsInfo SET MemberOrder=@0 WHERE UserID=@UserID
      END

      INSERT INTO QPAccountsDB.dbo.UserLoginRecord3(UserID,ip,loginFrom,Machine,Area,[Version]) values(@UserID,@strClientIP,@strLogFrom,@strMachineID,0,@dwVers    ion)

END
RETURN 0

what case the reason to the result?did i write the wrong code? please help me out

thx

proudlily commented 7 years ago

i saved this problem

my code is below:

str := `Exec QPAccountsDB.dbo.GO_EfficacyAccounts ?,?,?,?,?,?`
    stmr, err := s.DBGT.Db.Prepare(str)
    if err != nil {
        fmt.Println("Prepare", err)
        return
    }
    _, err = stmr.Exec(1000003, "moon far away", "11", 1, 2, 3)
    if err != nil {
        fmt.Println("exec :", err)
        return
    }
    fmt.Println("success")
alexbrainman commented 7 years ago

Here https://github.com/alexbrainman/odbc/blob/master/mssql_test.go#L1364-L1394 is another example of calling MS SQL Server stored procedure from go.

Alex

proudlily commented 7 years ago

thx @alexbrainman