简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM