Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Iterate through a collection of files in T-SQL

Posted on 2014-01-04
4
501 Views
Last Modified: 2014-01-04
I want to iterate through all the jpeg files in a folder, inserting them into a filestream column in SQL Server. I have a stored procedure to insert the files, but I don't know how to read through a series of folders and files using T-SQL.
0
Comment
Question by:TimHudspith
  • 2
4 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 39755855
you can achive this by using xp_cmdshell commands and bulk copy.
0
 

Author Comment

by:TimHudspith
ID: 39755860
Can you give me an example?
0
 
LVL 26

Expert Comment

by:tigin44
ID: 39755897
0
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 500 total points
ID: 39755911
You can try the following code:

IF OBJECT_ID('tempdb..#Files') IS NOT NULL DROP TABLE #Files;

CREATE TABLE #Files
    (
      ID INT IDENTITY(1, 1) ,
      Subdirectory NVARCHAR(512) ,
      Depth INT ,
      IsFile BIT
    );

INSERT  #Files( Subdirectory, Depth, IsFile )
EXEC master.sys.xp_dirtree 'C:\', 1, 1;

DECLARE @ID INT        
DECLARE FileCursor CURSOR FAST_FORWARD
FOR
    SELECT  ID
    FROM    #Files
    WHERE   IsFile = 1
    ORDER BY ID

OPEN FileCursor
FETCH NEXT FROM FileCursor INTO @ID

WHILE @@FETCH_STATUS = 0 
    BEGIN   
        DECLARE @FileName VARCHAR(256)
        SELECT  @FileName = Subdirectory
        FROM    #Files
        WHERE   ID = @ID

        PRINT @FileName

	FETCH NEXT FROM FileCursor INTO @ID;
    END
CLOSE FileCursor;
DEALLOCATE FileCursor;

Open in new window


instead of print @filename write your own code for each iteration.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Encrypting SQL Server 2014 or SQL Server 2016 4 30
Negative isnull? 3 15
HTML <font style="color:red"> 9 32
SQL Availablity Groups Shared Path 2 14
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question