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

  • SEARCH
  • Home
  • 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 7672991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:24:38+00:00 2026-05-31T16:24:38+00:00

I have a bunch of CSV files to import and so I’ve created BCPFORMAT

  • 0

I have a bunch of CSV files to import and so I’ve created BCPFORMAT files in XML. These are all working well and I can import the data correctly.

I’m now trying to create a stored procedure (which I’ve never done before) that accepts three parameters:
1. The table name
2. The path to the CSV file
3. The path to the BCPFORMAT file

CREATE PROC dbo.usp_import_csv 
    @table_name nvarchar(100),      /* name of table to import into */
    @csv_path nvarchar(1000),       /* path to csv file */
    @bcp_format_path nvarchar(1000) /* path to BCPFORMAT file */
AS
BEGIN
    DELETE FROM @table_name
    BULK INSERT @table_name
        FROM  @csv_path
        WITH (FIRSTROW = 1 , FORMATFILE = @bcp_format_path)

END

EXEC dbo.usp_import_cttp 'dbo.new_table', 'C:\temp\new_table.csv', 'C:\Temp\new_table.xml'

I get the message Incorrect syntax near '@table_name'. so I’m obviously not doing something quite right. But I can’t figure out what that is…

ANSWER DERIVED FROM BELOW

CREATE PROC dbo.usp_import_cttp
    @table_name nvarchar(100),      /* table to import into */ 
    @csv_path nvarchar(1000),       /* folder containing latest NZULM data files */
    @bcp_format_path nvarchar(1000) /* folder containing BCPFORMAT files used to explain csv files */
AS
BEGIN
    EXEC sp_executesql 'DELETE FROM @table_name', @table_name;

    EXEC sp_executesql 'BULK INSERT @table_name
                        FROM  @csv_path
                        WITH (FIRSTROW = 1 , FORMATFILE = @bcp_format_path)', 
                        @table_name, 
                        @csv_path,
                        @bcp_format_path;

END
  • 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-31T16:24:39+00:00Added an answer on May 31, 2026 at 4:24 pm

    You can’t put table names into variables. If you want to do that, your only resort is dynamic sql: building the query in a string variable. The good news is that you can at least still use sp_executesql for the other parameters.

    For security purposes, I like to also run a query against the information_schema to make sure the supplied table name is valid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of csv files. Each has data from a different period:
I have a bunch of CSV files. In some of them, missing data are
I have a bunch of gzipped CSV files that I'd like to open for
I am working on some data conversion. I asked for files in csv format
I have to read a bunch of .CSV files with dynamic file names from
I loaded a bunch of CSV files into R. I have questions about how
I have two .csv files, headers.csv and corrected.csv . The headers.csv has all the
I have a bunch of pretty large CSV (comma separated values) files and I
I have a folder that contains a bunch of csv files and I want
I want to import a bunch of xml data in weka. Is there a

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.