0
SET @fullPath = 'D:\Hosting\Project\Content\Order\ImportTest.txt'

SET @command = N'select @data_count =(select len(BulkColumn) - len(replace(BulkColumn, 
char(10), '')) + 1
from OPENROWSET(BULK ''' + @fullPath + ''',
SINGLE_CLOB) ROW_SET'

EXEC(@command)

The commands above are generating the following error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'D:'.

Msg 105, Level 15, State 1, Line 2
Unclosed quotation mark after the character string ', SINGLE_CLOB) ROW_SET'.

How do I fix the above issue?

1
  • How do I fix the above issue? You can't fix what you cannot see. To debug dynamic sql you must print or select the string that your code generates so you can actually examine the query, see the incorrect syntax, and then fix your code. Commented Nov 2, 2021 at 11:20

2 Answers 2

1

Just as you escaped the single quotes in BULK ''' + @fullPath + ''' you have to do it here: char(10), ''''.

Sign up to request clarification or add additional context in comments.

Comments

0

As well as the double-quotes, you also need to properly escape the filename using QUOTENAME

SET @fullPath = 'D:\Hosting\Project\Content\Order\ImportTest.txt';

SET @command = N'
select
  @data_count = len(BulkColumn) - len(replace(BulkColumn, char(10), '''')) + 1
from OPENROWSET(BULK ' + QUOTENAME(@fullPath, '''') + ',
              SINGLE_CLOB) ROW_SET
';

EXEC sp_executesql @command;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.