How to store image in SQL Server database tables column

292,800

give this a try,

insert into tableName (ImageColumn) 
SELECT BulkColumn 
FROM Openrowset( Bulk 'image..Path..here', Single_Blob) as img

INSERTING

enter image description here

REFRESHING THE TABLE

enter image description here

Share:
292,800
VINNUSAURUS
Author by

VINNUSAURUS

Updated on December 14, 2020

Comments

  • VINNUSAURUS
    VINNUSAURUS over 3 years

    I Have a table named FEMALE in my database. It has ID as Primary Key, it has an Image column.

    My Question is how do I store an image using a SQL Query?

  • VINNUSAURUS
    VINNUSAURUS about 11 years
    Please explain what is bulkcolumn ?
  • John Woo
    John Woo about 11 years
    bulkcolumn is part of the syntax.
  • VINNUSAURUS
    VINNUSAURUS about 11 years
    I Tried this INSERT INTO Female (Image) SELECT BulkColumn FROM Openrowset(BULK 'E:\Vinay\New Folder (2)\ileana00002.jpg ',Single_Blob)as img , but it is giving error Msg 515, Level 16, State 2, Line 1 Cannot insert the value NULL into column 'ID', table 'PMB.dbo.Female'; column does not allow nulls. INSERT fails. The statement has been terminated.
  • John Woo
    John Woo about 11 years
    the reason for that is your column ID is a NON_NULLABLE column. is it an integer column and set as identity? if not, then you need to specifically define column in your INSERT statement.
  • VINNUSAURUS
    VINNUSAURUS about 11 years
    Please show me how to do that iam new to SQL and i need it immediatly.
  • VINNUSAURUS
    VINNUSAURUS about 11 years
    my ID column is PK and incriments 1 on each entry
  • VINNUSAURUS
    VINNUSAURUS about 11 years
    OOPS THANK YOU it worked :D i did not set identity incriment and seeding property for auto incriment, but it worked thanks for the help :)
  • VINNUSAURUS
    VINNUSAURUS about 11 years
    Can you please show me how to insert other column Details too with the above query, that is with the image how to insert into other rows at the same time ?
  • John Woo
    John Woo about 11 years
  • Ads
    Ads almost 11 years
    @JW웃 what is the format its stored in the image column? is it Base64 or any other format?
  • Ali.Rashidi
    Ali.Rashidi over 7 years
    when I use WHERE in the query to insert image to specified rows it does not recognize table column names. why?
  • dosaki
    dosaki over 6 years
    The path needs to be local to the machine holding SQL Server