madhukar mohan
madhukar mohan

Reputation: 37

How to concatenate a part of filename in SQL?

I want to concatenate a part of filename in SQL.

 BULK INSERT #NewSegments    
 FROM 'E:\scratch\AT.txt'

 WITH (
         FIELDTERMINATOR ='\t',
         ROWTERMINATOR = '\n'
       )

I want to replace AT with a parameter @CountryCode.

Below SQL is not working.

 BULK INSERT #NewSegments
 FROM 'E:\scratch\' + @CountryCode + '.txt'

 WITH (
         FIELDTERMINATOR ='\t',
         ROWTERMINATOR = '\n'
       )

Upvotes: 1

Views: 560

Answers (1)

Kannan Kandasamy
Kannan Kandasamy

Reputation: 13969

you might require to go for dynamic sql as below:

Declare @Query Nvarchar(max)

Set @Query = 'BULK INSERT #NewSegments FROM ''E:\scratch\'+@CountryCode+'.txt'''
WITH ( FIELDTERMINATOR =''\t'', ROWTERMINATOR = ''\n'' ) '

exec sp_executeSql @Query

Upvotes: 3

Related Questions