Inserting an Image from Oracle into SQL Server

In the example, mssqllink is the name of a database link that uses DG4ODBC and Easysoft's SQL Server ODBC driver to connect to SQL Server.

CREATE DIRECTORY IMAGE AS '/tmp';

DECLARE
  curid NUMBER;
  execid NUMBER;
  src_file BFILE:=NULL;
  dst_file BLOB;
  lgh_file BINARY_INTEGER;
  file_status boolean:=false;
  amt BINARY_INTEGER := 2000; -- The maximum image size for this example is 2K.
  buf RAW(2000);
  bound_raw long raw;
  pos INTEGER := 1;

BEGIN
src_file := BFILENAME('IMAGE', 'test.jpg');
file_status := dbms_lob.fileexists( src_file )=1;

if file_status then

  -- open the file
  dbms_lob.createtemporary(dst_file, true, dbms_lob.call);
  dbms_lob.fileopen (src_file);

  -- determine length
  lgh_file := dbms_lob.getlength (src_file);
  dbms_output.put_line('Size : ' || lgh_file);
  if lgh_file<2001 then
      -- Read the file
      -- dbms_lob.loadfromfile(dst_file, src_file, lgh_file);
      Begin
        LOOP
          dbms_output.put_line('Pos : ' || pos);
          dbms_lob.read(src_file, amt, pos, buf);
          bound_raw :=bound_raw || buf;
          -- Process contents of buffer
          pos := pos + amt;
        END LOOP;
        EXCEPTION
            WHEN NO_DATA_FOUND
            THEN
                BEGIN
                    dbms_output.put_line('End of data reached');
                    dbms_lob.fileclose(src_file);
                END;
      -- Insert the blob field
      end;
      dbms_output.put_line('Buffer : ' || buf);
      curid := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@mssqllink; -- Replace this with the name of your linked table
      DBMS_HS_PASSTHROUGH.PARSE@mssqllink(curid,'insert into blb values (1,?)'); -- Destination table in SQL Server
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE_RAW@mssqllink(curid, 1, buf );

      execid := DBMS_HS_PASSTHROUGH.EXECUTE_NON_QUERY@mssqllink(curid);
      DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@mssqllink(curid);

      commit;
      dbms_output.put_line('Data sent to Easysoft SQL Server ODBC driver');
    else
        dbms_output.put_line('File size is too big for Oracle');
    end if;
else
  dbms_output.put_line('Can not open file.');
end if;

END;
/