go-mssqldb icon indicating copy to clipboard operation
go-mssqldb copied to clipboard

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

Open boazlautman opened this issue 3 years ago • 2 comments

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?

boazlautman avatar Nov 07 '20 08:11 boazlautman

` 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

fhquthpdw avatar Nov 23 '20 23:11 fhquthpdw

@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))

kardianos avatar Dec 11 '20 14:12 kardianos