denisenkom / go-mssqldb

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

How can i execute a simple stored procedure which accept 2 params and returns a table?? #616

Open boazlautman opened 4 years ago

boazlautman commented 4 years ago

With nodejs it is pretty simple In less than 5 minutes i succedded to execute sp https://www.npmjs.com/package/mssql

try { await conx; const request = conx.request(); request.input('CompanyID', sql.Int, compID) request.input('IsActive', sql.Int, IsActive) recordsets = await request.execute('SP_MANAGEMENT_GET_SUPPLIERS_DATA_BY_COMP_ID'); //console.log(recordsets); return recordsets; } catch (error) { console.log(error); // handle error here }

what is the golang equivalent?

fhquthpdw commented 3 years ago

` execStr := fmt.Sprintf("exec SP_NAME '%s', '%s', %d", parameter0, parameter2, parameter3) rows, err := c.MssqlDB.Query(execStr) if err != nil { // handle the error }

for rows.Next() {
    if err = rows.Scan(&field0, & field1, &field2); err != nil {
        // handle the error
    }
    fmt.Print(field0, field1, field2)
}`

it works on my side

kardianos commented 3 years ago

@fhquthpdw Don't do this.

If your parameters are strings, you must escape the value, which is a simple function, but must be done. Alt, you can run:

db.Query("sp_name", sql.Named("Param1", value1), sql.Named("Param2", value2))