DECLARE @imgString varchar(800)
DECLARE @insertString varchar(3000)
DECLARE @imgNumber int
Declare @imgName varchar(100)
SET @imgNumber = 1
WHILE @imgNumber<> 101
BEGIN
SET @imgName = 'SELECT (items) FROM dbo.building_piclink'
SET @imgString = 'C:\Documents and Settings\Administrator\Desktop\photos\' + @imgName
SET @insertString = 'INSERT INTO dbo.building__ATTACH (DATA)
SELECT * FROM OPENROWSET(BULK N''' + @imgString + ''', SINGLE_BLOB) as tempImg'
SET @imgNumber = @imgNumber + 1
END
GO
I am having problems with the @imgName. I can’t figure out how to get the value from the select statement not the (items) like below:
C:\Documents and Settings\Administrator\Desktop\photos\SELECT (items) FROM dbo.building_piclink
Thank you!
Your code has several problems:
1) You’re selecting a file name from the view – but what if that view contains more than one entry?? Which filename are you selecting?? Your current code first of all doesn’t work at all the way it is, and even if it were working – you’re still potentially selecting hundreds of filenames into a single variable – which of course won’t work….
So you’ll need to fix this here first:
First of all – loose the single quotes:
But now – do you have a unique ID that you can select for? Or do you want to get just the first entry (whatever that is) ??
So either you need:
and fill in that
WHEREclause with a condition that guarantees to return just a single row, or useTOP 1:In that case – you’ll just get exactly one filename – if you don’t specify an
ORDER BY, then there’s no guarantee what you’ll get – maybe you’ll want to add aORDER BY DueDateor something to prioritize which file names you get first.2) Your code for loading the image data is non workable, either – what you need to do is build up the SQL statement as a string, and then execute it (called dynamic SQL) – something like this:
With these two fixes, you should be on the way to get this thing working