Need to Create File Listing through T-SQL

  • Hello all,

    I am having a problem:

    I have two tables:

    MAIN_TABLE

    ---------------------------------------------------

    ID | Title | Link

    ---------------------------------------------------

    1 | air11 | \\server1\FILE_MANAGEMENT\air\air11

    2 | air12 | \\server1\FILE_MANAGEMENT\air\air12

    3 | air13 | \\server1\FILE_MANAGEMENT\air\air13

    ---------------------------------------------------

    SUB_TABLE

    --------------------------

    ID | TitleID | FileName

    --------------------------

    1 | 1 | a1.dwg

    2 | 1 | a2.dwg

    3 | 2 | a5.dwg

    4 | 3 | a3.dwg

    --------------------------

    Now let me explain my problem, in the MAIN_TABLE, whenever I'll to insert TITLE & LINK, a procedure or a trigger should run to fill up the SUB_TABLE with the list of files provided in the LINK column of MAIN_TABLE. The LINK column basically contains the path of a directory, where there contains multiple files.

    I came to know how to generate directory listing from the following link:

    http://www.siccolo.com/Articles/SQLScripts/how-to-create-sql-to-create-directory.html

    But unable to understand to get file listing.

    Hope I can make you understand my query!

    Can you please help me on this?

    Regards,

    Daipayan

    Software Programmer


    Application has reported a 'Not My Fault' in module KRNL.EXE in line 0200:103F

    ----------------------------------------
    Daipayan
    A Beginner to the World of DBMS!
    ----------------------------------------
  • Hi,

    which version of SQL server you have?

    Do you know xp_cmdshell and sp_ListFiles ?

    You may find following article helpful

    Cheers

  • Hello MidBar,

    Am using MS SQL Server 2005 and I know about xp_cmdshell and sp_ListFiles:

    exec xp_dirtree '\\server1\FILE_MANAGEMENT\air\air11', 0, 1

    create table #BACKUPS

    (

    subdirectory varchar(255),

    depth int,

    isfile int

    )

    INSERT #BACKUPS exec xp_dirtree '\\server1\FILE_MANAGEMENT\air\air11', 0, 1

    SELECT * FROM #BACKUPS

    I know how to generate the File list, but here my problem is instead of typing the location manually, I want to pull the destination from the LINK column of MAIN_TABLE, which I am unable to understand how to do it and further also want to pull the MAIN_TABLE ID as a foreign key in the SUB_TABLE..:ermm:

    ----------------------------------------
    Daipayan
    A Beginner to the World of DBMS!
    ----------------------------------------
  • Hi,

    I have used the example metioned above. Hopefully, this will give you some idea to solve your problem. Ofcourse we can change/extend this simple logic further.

    USE tempdb

    GO

    IF OBJECT_ID('#main_table') is not null DROP TABLE #main_table

    IF OBJECT_ID('#sub_table') is not null DROP TABLE #sub_table

    SET NOCOUNT ON

    CREATE TABLE #main_table

    (

    main_id int identity(1,1),

    title varchar(500),

    link varchar(968),

    command varchar(56),

    is_processed bit,

    exec_cmd as (command+link)

    )

    CREATE TABLE #sub_table

    (

    sub_id int identity(1,1),

    main_id int,

    sub_file_name varchar(1024)

    )

    INSERT INTO #main_table (title, link, command, is_processed) VALUES ('Title1', '\\server1\FILE_MANAGEMENT\air\air11', 'dir /b ', 0) -- Change Path Accordingly

    INSERT INTO #main_table (title, link, command, is_processed) VALUES ('Title2', '\\server2\FILE_MANAGEMENT\air\air11', 'dir /b ', 0) -- Change Path Accordingly

    INSERT INTO #main_table (title, link, command, is_processed) VALUES ('Title3', 'C:\WINDOWS\', 'dir /b ', 0)

    DECLARE @FilesInAFolder TABLE (FileNamesWithFolder VARCHAR(500))

    WHILE (select COUNT(1) from #main_table where is_processed=0) > 0

    begin

    DECLARE @Command VARCHAR(1024), @v_main_id int

    select top 1 @v_main_id=main_id, @Command=exec_cmd from #main_table where is_processed = 0

    INSERT INTO @FilesInAFolder

    EXEC MASTER..xp_cmdshell @Command

    INSERT INTO #sub_table(main_id, sub_file_name)

    SELECT @v_main_id, FileNamesWithFolder

    FROM @FilesInAFolder

    WHERE FileNamesWithFolder is not null

    UPDATE #main_table

    SETis_processed = 1

    WHERE main_id = @v_main_id

    DELETE FROM@FilesInAFolder

    END

    GO

    SELECT * FROM #main_table

    SELECT * FROM #sub_table

    DROP TABLE #main_table

    DROP TABLE #sub_table

    For the newbies, please use following code to enable xp_cmdshell.

    GO

    sp_configure 'show advanced options',1

    GO

    RECONFIGURE

    GO

    sp_configure xp_cmdshell,1

    GO

    RECONFIGURE

    Cheers.

  • Thank You Sir.

    I have 2 Query on this:

    1: This T-SQL is not accessible in network, works only in Local Machine, if I tried with local path, I am getting "Access Denied" stored in sub_file_name column

    2: Can I automate this process of T-SQL through trigger??

    ----------------------------------------
    Daipayan
    A Beginner to the World of DBMS!
    ----------------------------------------
  • Hmmm...

    1: I have tested this with network paths and on local and works fine with me. May be you dont have necessary permission to read the file from network locations.

    2: Yes please give it a try in trigger.

    Cheers.

  • mail2payan (6/17/2011)


    Thank You Sir.

    1: This T-SQL is not accessible in network, works only in Local Machine, if I tried with local path, I am getting "Access Denied" stored in sub_file_name column

    I don't know if this is your problem or not but just to be sure...

    "Local Path" for the server is the physical drives on the server itself. If it needs to "see" something else, you have to use a UNC path and the account that SQL Server logs in as must have the privs to be able to "see" that UNC path.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Dear MidBar; Jeff Moden,

    Thanks for quick reply.

    I tried, but not working, but when am using the following procedure: exec xp_dirtree '\\server1\FILE_MANAGEMENT\air\air11', 0, 1, it's working properly, why is it so??

    ----------------------------------------
    Daipayan
    A Beginner to the World of DBMS!
    ----------------------------------------
  • mail2payan (6/19/2011)


    Dear MidBar; Jeff Moden,

    Thanks for quick reply.

    I tried, but not working, but when am using the following procedure: exec xp_dirtree '\\server1\FILE_MANAGEMENT\air\air11', 0, 1, it's working properly, why is it so??

    Apologies for the week long delay. I lot track of this post.

    Can SQL Server actuall "see" the path you've used? If not, you may need to setup a share on \\server1 and grant SQL Server the rights to read whatever share you setup.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • This is actual path of one of the sub-folder:

    \\pinnacleserver1\FILE MANAGEMENT\asg\asg1

    Root Folder is: \\pinnacleserver1\FILE MANAGEMENT

    How to set share access policies in MS SQL Server??

    ----------------------------------------
    Daipayan
    A Beginner to the World of DBMS!
    ----------------------------------------
  • mail2payan (6/26/2011)


    How to set share access policies in MS SQL Server??

    You don't. You have to set the share at the OS level and the share needs to be for whatever login SQL Server uses for a login.

    If you have an OPs department, ask them for some help on this.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Ok sir, I'll do it.

    Thanks again!

    ----------------------------------------
    Daipayan
    A Beginner to the World of DBMS!
    ----------------------------------------

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply