There is an old MSSQL database and I need to make contact with it. The connection to it happens without any problems, but some kind of trouble happens with requests. There is a table [Employees] with the fields: id, Name, E-mail and for example, I need to pull out the name knowing only the mail. This is what my method looks like:

 func (w *Wrapper) GetAuthorInIntexOrder(email string) (string) { row := IntexDB.QueryRow("SELECT Name FROM [Сотрудники] WHERE [E-mail] = $1", email) var name string err := row.Scan( &name, ) if err != nil { log.Warn("[database]GetAuthorInIntexOrder->row.Scan: ", err.Error()) return "" } return name } 

In response, I get the error: "[database]GetAuthorInIntexOrder->row.Scan: mssql: Cannot convert a char value to money. The char value has incorrect syntax." Name is nvarchar type and I don’t understand why money is being converted at all, it's just a string. I use the github.com/denisenkom/go-mssqldb driver. It is interesting that if we remove the WHERE condition, the query will be executed and I will get the 1st element in the table. What do you think could be the problem?

  • There is a doubt that the query in QueryRow is written correctly. $ 1 is a value of type money - Denis Rubashkin February
  • And why did you decide that the problem is with Name conversion? Perhaps it with substitution of $ 1 rake? Just in case - do not be lazy in the query text to enclose Name with square brackets, and $ 1 with single quotes ... first, separately. - Akina
  • Yes, the problem was in the substitution of $ 1. I substituted real mail instead of $ 1 (SELECT [Name] FROM [Employees] WHERE [E-mail] = 'test@test.ru') and the request worked. I cannot wrap single quotes at $ 1 because then it will not be a substitution. I can not understand how to correctly transfer the data. - Alexander Bondarenko
  • I cannot wrap single quotes at $ 1 because then it will not be a substitution. ??? And you just try ... row := IntexDB.QueryRow("SELECT Name FROM [Сотрудники] WHERE [E-mail] = '$1' ", email) - Akina
  • ... или row: = IntexDB.QueryRow ("SELECT Name FROM [Employees] WHERE [E-mail] = '?'", email) `... - Akina

1 answer 1

I am sure that the value of the email variable is not included in the request, but the value of $ 1 of money type is sent, so when you try to convert the value from the [E-mail] column, you get an error.

Try:

 row := IntexDB.QueryRow("SELECT Name FROM [Сотрудники] WHERE [E-mail] = @p1", email) 
  • Your option does not fit, the result is an error: mssql: Incorrect syntax near '?'. - Alexander Bondarenko
  • But this option works: IntexDB.QueryRow("SELECT [Name] FROM [Сотрудники] WHERE [E-mail] = '" + email + "'") - Alexander Bondarenko
  • Try again with @p1 - Denis Rubashkin
  • Thank you very much! The @ p1 option worked. - Alexander Bondarenko