Home > Software design >  Saving PDF file in SQL Server using Golang
Saving PDF file in SQL Server using Golang

Time:04-24

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!

CodePudding user response:

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

  • Related