pass string with space and keyword

coool sweet 61 Reputation points
2022-03-30T17:41:52.04+00:00

hi

i need to pass path to my stored porc.

i have path liek this

C:\ data care\test\frt.xls

i am getting error as i believe [DATA] is keyword and space is the issue .

how to solve it

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,454 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tom Phillips 17,686 Reputation points
    2022-03-30T17:55:26+00:00

    Strings in SQL Server should be surrounded by single quotes, 'C:\ data care\test\frt.xls'

    0 comments No comments

  2. Bert Zhou-msft 3,406 Reputation points
    2022-03-31T05:00:20.537+00:00

    Hi,@coool sweet

    Welcome to Microsoft T-SQL Q&A Forum!

    You could try this code to test :

    CREATE PROCEDURE [importFile] (@filePath VARCHAR(MAX))  
    AS  
    BEGIN  
        CREATE TABLE #Temp  
        (  
          column1 int,  
          column2 varchar(10)  
        )  
      
        DECLARE @SQL NVARCHAR(MAX) = ''  
        SET @SQL = N'  
        BULK INSERT #Temp  
          FROM ''' + @filePath + '''  
          WITH (  
            FIELDTERMINATOR = '','',  
            ROWTERMINATOR = ''\n''  
          )'  
      
         -- ...  
      
         EXEC sp_executesql @SQL  
    END  
      
    -- to run it:  
    EXEC importFile 'C:\data care\test\frt.xls'  
    

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments