Saving PDF file in SQL Server using Golang

Issue

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 below error.

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!

Solution

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

Answered By – Majid

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.