1

I'm having issue in saving a PDF file in SQL Server database using stored procedure in Golang. Below is the code.

tsql := fmt.Sprintf("DECLARE @tmp varbinary(max);"+
    "SET @tmp = CAST('%s' as varbinary(max));"+
    "EXEC BP_AddCorrespondenceIn @PatientID=1, @ContactName='Test', @Subject='First Test',"+
    "@Category='Report', @DocType='PDF', @Content = @tmp", content)

// Execute query
rows, err := db().Query(tsql)

Here the content is the [ ]byte. When I run the program the query executes and I got the error below:

mssql: '3�Ze� #��!~T��ϔljQ*���f1-~L���^ը;s;���.�)�[P�hjDN��J�.1��W�Zt���xq�\r���ן�)N���=df' is an invalid name because it contains a NULL character or an invalid unicode character.

Thank you!

3
  • 2
    What does CAST('%s' as varbinary(max)) do? If its a binary file, you shouldn't need to cast it to anything.
    – Dale K
    Apr 21, 2022 at 2:01
  • 1
    AFAIK go-mssqldb doesn't currently support varbinary(max) parameters. Consider using another library that does, otherwise you're going to have to serialize the PDF content as a varbinary literal (0x12345678ab...) and inject it into the SQL statement which is, at best, questionable from a security standpoint. Doing so will also cause cache bloat because there'll be a new plan generated and cached for each call. Apr 21, 2022 at 2:50
  • I fixed the problem by changing the stored procedure exec method to _, err := db().Exec(tsql, content) and varbinary(max) conversion to tmp = CAST(? as varbinary(max)); Thank you for showing me the way to solve the issue guys!
    – Majid
    Apr 21, 2022 at 11:34

1 Answer 1

0

I fixed the problem by changing the stored procedure exec method to _, err := db().Exec(tsql, content) and varbinary(max) conversion to tmp = CAST(? as varbinary(max));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.