Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Iterate through a collection of files in T-SQL

Posted on 2014-01-04
4
Medium Priority
?
508 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

810 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