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!
CAST('%s' as varbinary(max))
do? If its a binary file, you shouldn't need to cast it to anything.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.