T-SQL: Files import and export using T-SQL for SQL Server

Поділитися
Вставка
  • Опубліковано 10 гру 2024
  • DECLARE @FOLDER as nvarchar(200)
    DECLARE @FILE as nvarchar(200)
    DECLARE @SQL as nvarchar(200)
    CREATE TABLE MyPictures (
    ID int,
    Folder nvarchar (200),
    FName nvarchar (200),
    Img varbinary (max)
    )
    INSERT INTO MyPictures
    SELECT 1,'C:\!all\IDEA_A\2 images\','arrow.png', BulkColumn
    FROM OPENROWSET (BULK 'C:\!all\IDEA_A\2 images\arrow.png', SINGLE_BLOB) as image
    SET @FOLDER = 'C:\!all\IDEA_A\2 images\'
    SET @FILE = 'SQL_JOINS.jpg'
    SET @SQL= ' INSERT INTO MyPictures
    SELECT 2,'''+@FOLDER+''','''+@FILE+''', BulkColumn
    FROM OPENROWSET (BULK '''+@FOLDER+@FILE+''', SINGLE_BLOB) as image'
    Exec(@SQL)
    SELECT *
    FROM MyPictures
    --DROP TABLE MyPictures
    -------------------------------------------------------------------------
    --sp_configure 'show advanced options', 1;
    --GO
    --RECONFIGURE;
    --GO
    --sp_configure 'Ole Automation Procedures', 1;
    --GO
    --RECONFIGURE;
    --GO
    ---------------------------------------------
    DECLARE @OUTPUT_FOLDER_FILE as nvarchar(200)
    DECLARE @INIT int
    DECLARE @DATA varbinary(max)
    SELECT
    @OUTPUT_FOLDER_FILE = [Folder] + 'out_' + [FName],
    @DATA=Img
    FROM MyPictures
    Where ID=1
    EXEC sp_OACreate 'ADODB.Stream', @INIT OUTPUT; -- An instace created
    EXEC sp_OASetProperty @INIT, 'Type', 1;
    EXEC sp_OAMethod @INIT, 'Open'; -- Calling a method
    EXEC sp_OAMethod @INIT, 'Write', NULL, @DATA; -- Calling a method
    EXEC sp_OAMethod @INIT, 'SaveToFile', NULL, @OUTPUT_FOLDER_FILE, 2; -- Calling a method
    EXEC sp_OAMethod @INIT, 'Close'; -- Calling a method
    EXEC sp_OADestroy @INIT; -- Closed the resources
    -------------------------------------------------------------------------
    DROP TABLE MyPictures

КОМЕНТАРІ • 4

  • @heliotavares6402
    @heliotavares6402 3 роки тому

    Great! Thanks so much.

  • @Fcb447
    @Fcb447 3 роки тому

    awesome! thanks a lot

  • @mkhalidumer
    @mkhalidumer 4 роки тому +1

    Thank you Alex, you are a GEM

  • @ndungustevens
    @ndungustevens 5 років тому +1

    Fantastic, this is only for advanced user.