March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
You can download the example Power BI solution from the end of this blog post.
USE master
GO
-- This is needed only if you use FILESTREAM:
-- EXEC sp_configure filestream_access_level, 2
-- RECONFIGURE
-- This is needed only if you use script to read file *names*
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
-- Create table for uploading files
USE AdventureWorks2017
GO
CREATE TABLE [dbo].[Pictures](
[pictureName] [nvarchar](40) NOT NULL,
[picFileName] [nvarchar](100) NULL,
[PictureData] [varbinary](max) NULL,
PRIMARY KEY CLUSTERED
(
[pictureName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
-- Create upload procedure for uploading files to DB, see more from https://www.mssqltips.com/sqlservertip/4963/simple-image-import-and-export-using-tsql-for-sql-server...
CREATE PROCEDURE dbo.usp_ImportImage (
@PicName NVARCHAR (100)
, @ImageFolderPath NVARCHAR (1000)
, @Filename NVARCHAR (1000)
)
AS
BEGIN
DECLARE @Path2OutFile NVARCHAR (2000);
DECLARE @tsql NVARCHAR (2000);
SET NOCOUNT ON
SET @Path2OutFile = CONCAT (
@ImageFolderPath
,'\'
, @Filename
);
SET @tsql = 'insert into Pictures (pictureName, picFileName, PictureData) ' +
' SELECT ' + '''' + @PicName + '''' + ',' + '''' + @Filename + '''' + ', * ' +
'FROM Openrowset( Bulk ' + '''' + @Path2OutFile + '''' + ', Single_Blob) as img'
EXEC (@tsql)
SET NOCOUNT OFF
END
GO
exec dbo.usp_ImportImage 'United States','C:\SqlData\Import','united-states-of-america-flag-icon-32.png'
exec dbo.usp_ImportImage 'Canada','C:\SqlData\Import','canada-flag-icon-32.png'
exec dbo.usp_ImportImage 'United Kingdom','C:\SqlData\Import','united-kingdom-flag-icon-32.png'
USE AdventureWorks2017
GO
SET NOCOUNT ON
-- Deletes everything from the table!
TRUNCATE TABLE [dbo].[Pictures]
-- 1 - Variable declaration
DECLARE @dbName sysname
DECLARE @importPath NVARCHAR(500)
DECLARE @cmd NVARCHAR(500)
DECLARE @fileList TABLE (importFilename NVARCHAR(255))
DECLARE @importFile NVARCHAR(500)
-- 2 - Initialize variables
SET @importPath = 'C:\SqlData\Import\'
-- 3 - get list of files
SET @cmd = 'DIR /b "' + @importPath + '"'
INSERT INTO @fileList(importFilename)
EXEC master.sys.xp_cmdshell @cmd
-- 4 - Upload image
DECLARE importFiles CURSOR FOR
SELECT importFilename
FROM @fileList
WHERE importFilename IS NOT NULL -- importFilename LIKE '%.PNG'
OPEN importFiles
-- Loop through all the files
FETCH NEXT FROM importFiles INTO @importFile
WHILE @@FETCH_STATUS = 0
BEGIN
-- SET @cmd = 'EXEC dbo.usp_ImportImage ''' + LEFT(@importFile, CHARINDEX('.', @importFile)-1)
SET @cmd = 'EXEC dbo.usp_ImportImage ''' + LEFT(@importFile, CHARINDEX('-flag', @importFile)-1)
+ ''', ''C:\SqlData\Import'', ''' + @importFile + ''''
PRINT @cmd
FETCH NEXT FROM importFiles INTO @importFile
END
-- SELECT * FROM [dbo].[Pictures]
let
Source = Sql.Database("localhost", "adventureworks2017"),
dbo_Pictures = Source{[Schema="dbo",Item="Pictures"]}[Data],
dbo_Pictures2 = Table.AddColumn(dbo_Pictures, "encodedImage", each "data:image/png;base64," & Binary.ToText([PictureData], BinaryEncoding.Base64)),
#"Replaced Value" = Table.ReplaceValue(dbo_Pictures2,"-"," ",Replacer.ReplaceText,{"pictureName"}),
#"Capitalized Each Word" = Table.TransformColumns(#"Replaced Value",{{"pictureName", Text.Proper, type text}})
in
#"Capitalized Each Word"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.