Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:21:43+00:00 2026-05-16T17:21:43+00:00

I have a stored procedure called sp_BulkInsert that inserts one .csv file into my

  • 0

I have a stored procedure called “sp_BulkInsert” that inserts one .csv file into my database, where you specify the full path of the file when you execute it. I am trying to create another stored procedure called “sp_ResultsDump” where you specify the folder path, which then searches the folder for all .csv files, creates a table with file names, then loops through the rows of that table while executing “sp_BulkInsert” for each .csv file in the folder (the names of which are recorded in the previous table).

Here is the code:

--Step 0: Create Stored Procedure
CREATE PROCEDURE sp_ResultsDump
@PathFolder VARCHAR(2000)
AS

--Step 1: Create table of file names
IF EXISTS (SELECT 1 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    AND TABLE_NAME='Files')
    DROP TABLE Files
CREATE TABLE Files(FileID INT IDENTITY NOT NULL, FileName VARCHAR(max))
DECLARE @PathExec VARCHAR(1000)
SET @PathExec = "dir '"+@PathFolder+"'.csv /B"
INSERT INTO Files(FileName) EXEC master..xp_cmdshell @PathExec
DELETE Files WHERE FileName IS NULL

--Step 2: Get # of files, declare and initialize iterator
DECLARE @RowCount INT, @I INT
SET @RowCount = (SELECT COUNT(FileName) FROM Files)
SET @I = 1

--Step 3: Loop through the rows of a table and execute sp_ResultsDump for each file
WHILE (@I <= @RowCount)
BEGIN
        DECLARE @FileName VARCHAR(1000)  
        SELECT @FileName = FileName FROM Files WHERE FileID = @I
        SELECT @FileName = @PathFolder+@FileName
        EXEC sp_BulkInsert @FileName
        SET @I = @I  + 1
END

I have confirmed that Steps 1-3 work when I specify the folder (without creating a stored procedure or a dynamic @variable), however storing the @PathFolder seems to be the problem. For example, I want to grab all .csv files from C:\, and each @FileName through the loop will loop through the file names contained in table Files, column FileName.

What I want to do is to be able to execute the following code so that I can get all .csv files in a specified folder and successfully bulk insert them into my database:

EXEC sp_ResultsDump 'c:\'

The reason for this is because the folder path may change later, and I want the user to be able to specify it.

I believe that “SELECT @FileName = @PathFolder+@FileName” is incorrect, and I tried all sorts of combinations of quotation marks and +’s. Steps 1 and 3 both seem to have problems with @PathFolder.

I guess I just need help with my while loop, because I think if my while loop is correct, this should be good.

Any suggestions? Simple syntax error somewhere? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:21:44+00:00Added an answer on May 16, 2026 at 5:21 pm

    I think your problem is with the following SET command

    SET @PathExec = "dir '"+@PathFolder+"'.csv /B"
    

    It appears to be mixing the double-quotes and single-quotes. Try changing it to this

    SET @PathExec = 'dir "' + @PathFolder + '.csv" /B'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Stored Procedure called spGetOrders which accepts a few parameters: @startdate and
I am writing a stored procedure where I have an input parameter called my_size
I have stored procedures in SQL Server T-SQL that are called from .NET within
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a stored procedure that consists of a single select query used to
I have a stored procedure that returns multiple tables. How can I execute and
I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
I have a stored procedure with the following header: FUNCTION SaveShipment (p_user_id IN INTEGER,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.