Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

tsql - T-SQL: issue with string concat

I have a set of audio files with names GreenLine1.mp3, GreenLine2.mp3 e.t.c. I'm going to write them into a table as BLOB (I use MS SQL Server'08), here's my sql request:

DECLARE @aud AS VARBINARY(MAX)
DECLARE @num AS INT    
-- Load the audio data
SET @num=1
WHILE (@num<38)
BEGIN;

SELECT @aud = CAST(bulkcolumn AS VARBINARY(MAX))
      FROM OPENROWSET(
            BULK
            'C:UsersIlyafolderGreenLine' + CAST(@num AS VARCHAR) + '.mp3',
            SINGLE_BLOB ) AS x

-- Insert the data to the table          
INSERT INTO Mb2.dbo.Audios (Id, [Content])
SELECT NEWID(), @aud
SET @num = @num + 1
END;

I have an error: Incorrect syntax near '+', expecting ',' or ')'.

If I try to write

'C:UsersIlyafolderGreenLine' + CAST(@num AS VARCHAR) + '.mp3'

into a variable and put it after BULK, I get Incorrect syntax near @variable, expected STRING, or TEXT_LEX

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't parametrise or concatenate the parameters of OPENROWSET. It is constant values only.

You'll have to use dynamic SQL and a temp table, or consider using SSIS for example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...