Zame
Zame

Reputation: 320

Concatenate in SQL Server

I have the following code in SQL

DECLARE c CURSOR FOR select  MSISDN FROM
OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=' + @Path + ';HDR=YES', 'SELECT MSISDN FROM [sheet1$]')

i want to concatenate the path in the database , but the concatenation isn't working , any idea ?

Upvotes: 0

Views: 1016

Answers (1)

Tim
Tim

Reputation: 28530

You can't do string concatenation in OPENROWSET - the command expects string literals. I recently had a project at work converting some old SQL that used OPENROWSET and ran into that issue.

One way around this is by using OPENROWSET to dump the data into a table variable, and then declare your cursor from the table variable. Something like this (not tested):

DECLARE @data AS TABLE(MSISDN VARCHAR(255))

DECLARE @sql AS VARCHAR(4000)

SET @sql = 'SELECT MSISDN FROM '
SET @sql = @sql + ' OPENROWSET(''Microsoft.ACE.OLEDB.12.0'','
SET @sql = @sql + '''Excel 12.0;Database=' + @Path + ';HDR=YES'','
SET @sql = @sql + '''SELECT MSISDN FROM [sheet1$]'')'

INSERT INTO @data
EXECUTE sp_executesql @sql

DECLARE c CURSOR FOR SELECT MSISDN FROM @data

Upvotes: 1

Related Questions