Search code examples
gosqlx

Update MS SQL Table


I have a MSSQL database that I am trying to update using sqlx. I have successfully been able to read the table, but I have yet to update the table.

import (
_ "github.com/alexbrainman/odbc"
"github.com/jmoiron/sqlx"
)

func main() {
    SQLServer := "my_server"
    StdDB := "standard"
    TrkgTbl := "Tracking"

    databaseId := "2,3,582,1211,1791,39,1179,40,2082,55,107,1357,1931,60," +
        "389,393,2290,396,407,487,2271,670,702,1206"

    db, err := sqlx.Open("odbc", `Driver=SQL Server;Server=`+
        SQLServer+`;Database=`+StdDB+
        `;Trusted_Connection=yes;`)
    if err != nil {
        fmt.Println("Connection Failed ", err)
    }
    stmt := `UPDATE ` + TrkgTbl + `SET Status = 'Not Started'` +
        `, StaticCopyDate = null WHERE databaseid in (` + databaseId + `)`
    _, err = db.Exec(stmt)
    if err != nil {
        panic(err)
    }
    defer db.Close()
}

Returns:

panic: SQLExecute: {42000} [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Status'.
{42000} [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.

goroutine 1 [running]:
main.main()
        C:/Users/Clarkus978/go/src/updateSql.go:27 +0x33f
exit status 2

I have also tried placeholders (?), but nothing works. I'm not sure how to get around the issue.


Solution

  • `UPDATE ` + TrkgTbl + `SET
    

    needs to be

    `UPDATE ` + TrkgTbl + ` SET