Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database in which contain product details The product code, name etc are the columns, I have the product images in the system in a folder I need to add the images directly to the database according to the proper rows regarding the product code, The product code and images are sam, the product code is like 110-1,110-2 etc, and image names are 110-1jpg,110-2jpg etc I have a program but its not updating all the rows pls help
SQL
DECLARE @CODE varchar

DECLARE image_cursor CURSOR FOR

SELECT CODE FROM MyMast WHERE img IS NULL

OPEN image_cursor;

FETCH NEXT FROM image_cursor

INTO @CODE;

WHILE @@FETCH_STATUS = 0

BEGIN

DECLARE @sql VARCHAR(MAX)

DECLARE @imagePath VARCHAR(255)

SET @imagePath = 'D:\images\' + RTRIM(LTRIM(@CODE)) + '.jpg'

SET @sql = 'UPDATE Mymast'

SET @sql = @sql + 'SET img = (SELECT BulkColumn FROM OPENROWSET( BULK ''' + @imagePath + ''', Single_Blob) AS Picture), SET PictureFileName = ' + @imagepath

SET @sql = @sql + 'WHERE CODE = ''' + @CODE + ''';'

BEGIN TRY

EXECUTE sp_executesql @sql

END TRY

BEGIN CATCH

END CATCH

FETCH NEXT FROM image_cursor

INTO @CODE;

END

CLOSE image_cursor;

DEALLOCATE image_cursor;

SELECT CODE, img FROM MyMast WHERE img IS NOT NULL
Posted
Updated 6-Jan-15 21:56pm
v2

1 solution

A few minor errors need to be addressed.
SQL
--length of variable required
DECLARE @CODE varchar(20);

SQL
--string concatenation issues
--also an addition SET to be removed from SET PictureFileName
--the initial @imagePath has single quotes surrounding it on the generated sql but the second one does not
SET @sql = 'UPDATE Mymast'
SET @sql = @sql + 'SET img = (SELECT BulkColumn FROM OPENROWSET( BULK ''' + @imagePath + ''', Single_Blob) AS Picture), SET PictureFileName = ' + @imagepath
SET @sql = @sql + 'WHERE CODE = ''' + @CODE + ''';'
--on the concatenation side the above will produce UPDATE MymastSET and the WHERE will be appended to generated @imagepath

SQL
--the @sql needs to be of nvarchar for the statement to be executed using sp_executesql
EXECUTE sp_executesql @sql
--or use
EXEC (@sql);

sp_executesql (Transact-SQL): http://msdn.microsoft.com/en-us/library/ms188001.aspx[^]

I believe all the issues have been pointed out.
Consider using an alternative to a cursor as they are considered bad practice:
http://stackoverflow.com/questions/58141/why-is-it-considered-bad-practice-to-use-cursors-in-sql-server[^]

Also possibly get in the habit of using the semicolon statement terminator:
Features Not Supported in a Future Version of SQL Server[^]

SQL
--here are the changes as specified above
--only slight modification of your original code
DECLARE @CODE varchar(50);
DECLARE image_cursor CURSOR FOR
 	SELECT CODE FROM MyMast WHERE img IS NULL
 	OPEN image_cursor;
 		FETCH NEXT FROM image_cursor
 			INTO @CODE;
 			
		WHILE @@FETCH_STATUS = 0 BEGIN
			DECLARE @sql VARCHAR(MAX);
			DECLARE @imagePath VARCHAR(255);
			SET @imagePath = 'D:\images\' + RTRIM(LTRIM(@CODE)) + '.jpg';
			SET @sql = 'UPDATE Mymast ';
			SET @sql = @sql + 'SET img = (SELECT BulkColumn FROM OPENROWSET( BULK ''' + @imagePath + ''', Single_Blob) AS Picture),  PictureFileName = ''' + @imagepath + ''' ';
			SET @sql = @sql + 'WHERE CODE = ''' + @CODE + ''';';
			BEGIN TRY
--				EXECUTE sp_executesql @sql
				EXEC (@sql);
			END TRY
			BEGIN CATCH
			END CATCH

			FETCH NEXT FROM image_cursor
				INTO @CODE;
		END;
	CLOSE image_cursor;
DEALLOCATE image_cursor;
SELECT CODE, img FROM MyMast WHERE img IS NOT NULL;
--if it does not work, ensure all images in CODE column are present in D:\images\ folder.
 
Share this answer
 
v2
Comments
Member 11357862 7-Jan-15 9:41am    
sir till now my issue is not reolved
jaket-cp 7-Jan-15 9:51am    
Okay so you have implemented the varchar(20) - that is good.
Did you implement the other points?
Member 11357862 7-Jan-15 9:54am    
ya I have removed the set before the picture file name. I didn't get the details abt string concatenation
jaket-cp 7-Jan-15 10:00am    
okay that is good.
The string concatenation, means all the string values joined together to make a longer string.
In this case the query which is stored in @sql.
Note when @sql is generated the result on a portion of that string will be MymastSET this is not good as no space between Mymast and SET, so this needs to be fixed by adding a space, also add spaces before the Where.
Member 11357862 7-Jan-15 10:24am    
ya I hv given space, but it doesn't resolved issue, can u pls give me any other code for inserting multiple images?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900