Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have sql code to insert images from my system to the database. In this code the file name is not printing. How can I print the file name?
SQL
CREATE TABLE [dbo].[Images](
     [id] [int] IDENTITY(1,1) NOT NULL,
     [Filename] varchar(100),

    [Path] varchar(200),

    [document] [image] NULL    )


DECLARE @imgString varchar(80), @imgNumber int
DECLARE @insertString varchar(3000)

SET @imgNumber = 13
WHILE (@imgNumber < 19)
BEGIN

    SET @imgString = 'D:\images\43-' + CONVERT(varchar,@imgNumber) + '.jpg'
    SET @insertString = N'INSERT INTO Images(document)
                        SELECT * FROM OPENROWSET(BULK N''' + @imgString + ''', SINGLE_BLOB) as tempImg'


    EXEC(@insertString)
    SET @imgNumber = @imgNumber + 1
 EXEC(@insertString)
END
select * from images



also for images that are not present in system it shows error
Posted

In your INSERT statement you're only defining the document field. Because of this all other fields are left empty so they get their default values.

Try changing your insert to something like
SQL
SET @insertString = N'INSERT INTO Images(filename, path, document)...

Just make sure that you provide corresponding values in the SELECT portion
 
Share this answer
 
Comments
Member 11357862 12-Jan-15 3:35am    
could u pls explain
Wendelius 12-Jan-15 3:52am    
The insert statement requires you to provide the values for specified columns. In your current insert statement you define value only to the image column and because of this the filename and path columns are left empty.

In order to define the value for filename in the insert statement you must define the column in the INSERT clause and also provide a value in the SELECT clause.

For information about the INSERT syntax, refer to INSERT (Transact-SQL)[^]
don't use Image Data-type in your apps.
before using data types is SQL Server, read this on -> Click Here
 
Share this answer
 

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