mssql BULK read an osfile and output

I am attempting to use the OPENROWSET(Bulk,... method to read an image, to be used as a column on a SQL View.

CREATE TABLE dbo.tblMyFiles
      (
      ID int NOT NULL IDENTITY (1, 1),
      FilePath varchar(255) NOT NULL
      )  ON [PRIMARY]
GO
INSERT INTO tblMyFiles (FilePath) VALUES ('c:\1\test.png')
GO


This works
 SELECT IMG=(SELECT CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET(BULK 'c:\1\test.png', SINGLE_BLOB ) AS x) FROM MyFiles WHERE ID=1

This does not work
 SELECT IMG=(SELECT CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET(BULK tblMyFiles.FilePath, SINGLE_BLOB ) AS x) FROM MyFiles WHERE ID=1

Server: Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'tblMyFiles'.


I've tried many different solutions.
 Quoting the tblMyFiles.FilePath...
 Putting it in an @Variable.

http://www.mssqltips.com/sqlservertip/1643/using-openrowset-to-read-large-files-into-sql-server/
http://www.sqlservercentral.com/Forums/Topic469199-5-1.aspx
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=143948

Any suggestions?
LVL 1
SirParadoxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
OPENROWSET BULK requires a hard-coded path name. I get around that with dynamic sql.
DECLARE @FIlePath varchar(255),
        @SQL      varchar(max)

SELECT @FilePath = FilePath
FROM   tblMyFiles
WHERE  ID = 1

SET @SQL = 'SELECT IMG=(SELECT CAST(bulkcolumn AS VARBINARY(MAX)) ' +
           'FROM OPENROWSET(BULK ''' + @FilePath + ''', SINGLE_BLOB ) AS x) ' +
           'FROM MyFiles WHERE ID=1'

EXEC (@SQL)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
SirParadox, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.