Search code examples
c#sql-serverstringquotation-marks

Error Message Unclosed quotation mark after the character string


I got this error message when I executed the query.

Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ''

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ''

My code:

DECLARE @imgLogo VARCHAR(50)
DECLARE @savPicture VARBINARY(MAX)
DECLARE @strPerson VARCHAR(50) = 'Max Verstappen'

SET @imgLogo = 'SET @SavPicture = (SELECT * FROM OPENROWSET (BULK N''C:\Program Files\Microsoft SQL Server\140\Couriers\ + TRIM (cast('+ @strPerson + '''as char(30))) + .jpg'

EXEC (@imgLogo)

Can someone help me out?


Solution

  • I fix it, it marks that error because there were a few characters with no comma or extra ). I also fix the other message that It will occur if you don't specify the SINGLE_NCLOB. The only issue that I give it to you its the problem with the scalar variable. If you like it please give me a vote, I solve what you ask, a little more but not everything.

    DECLARE @imgLogo VARCHAR(200)
    DECLARE @SavPicture VARBINARY(MAX)
    DECLARE @strPerson VARCHAR(50) = 'Max Verstappen'
    
    SET @imgLogo = 'SET @SavPicture = (SELECT * FROM OPENROWSET (BULK N''C:\Program Files\Microsoft SQL Server\140\Couriers\' + TRIM(cast(+ @strPerson as char(30))) + '.jpg'',SINGLE_NCLOB) as imagefile)'
    
    EXEC (@imgLogo)