denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.81k stars 493 forks source link

Stored Procedures not working in Linux version of SQL server? #783

Open TSS3005 opened 1 year ago

TSS3005 commented 1 year ago

Hi,

I tried to call a stored procedure like in your example but it doesn't work. Here is what I did on the SQL server:

SELECT @@version;

Microsoft SQL Server 2019 (RTM-CU18) (KB5017593) - 15.0.4261.1 (X64)    
Sep 12 2022 15:07:06    Copyright (C) 2019 Microsoft Corporation    
Developer Edition (64-bit) on Linux (Oracle Linux Server 8.7) <X64>

CREATE TABLE accounts1 (
    id INT,
    account varchar(100)
);

INSERT INTO accounts1 (
    id,
    account
)
VALUES(
    122,
    'Miller'
    ),
    (
    123,
    'Smith'    
    ),
    (
    124,
    'Johnson'    
    );   

CREATE OR ALTER PROCEDURE [dbo].[sp_RunMe](@ID INT, @Account VARCHAR(100) OUTPUT)
AS
BEGIN
    SET NOCOUNT ON;
    SELECT account FROM accounts1 WHERE id = @ID
END;

Here is my go code:

func main() {

    connectionString := fmt.Sprintf("user id=%s;password=%s;port=%s;database=%s", user, password, port, database)
    db, err := sql.Open("mssql", connectionString)
    if err != nil {
        fmt.Println(fmt.Errorf("error opening database: %v", err))
    }
    defer db.Close()
    err = db.Ping()
    if err != nil {
        fmt.Println("Could not ping database!")
    }
    fmt.Println("Database was pinged")

    var account = "abc"
    _, err = db.ExecContext(ctx, "sp_RunMe",
        sql.Named("ID", 123),
        sql.Named("Account", sql.Out{Dest: &account}),
    )
    fmt.Println(account)

If I try to run this I get this:

[tss@localhost mssql1]$ ./mssql1
Database was pinged
abc

Is the Linux version of the SQL server not supported or am I doing something wrong? I'm grateful for every helpful hint.

Thank you