Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlcmd script with spaces in filename

I have a simple SQLCMD script that includes some lines like this:

/* Load data into Exampletable */
BULK INSERT dbo.Example
    /* NOTE: I've tried single AND double quotes here. */
    FROM "C:\Example Filepath\test.csv"
    WITH 
    (
            /* skip the first row containing column names */
            FIRSTROW = 2,
            /* specify how fields are separated */
            FIELDTERMINATOR = '|',
            /* specify how lines end */
            ROWTERMINATOR = '\n' 
    )

When I run it on the command line, I get an error like this:

Sqlcmd: 'C:\Example': Invalid filename.

I think that having a space in the path is causing the path to be cut off, but I can't figure out a syntax that works. Does anybody have any experience with this?

like image 415
SuperNES Avatar asked Jan 18 '11 16:01

SuperNES


People also ask

How do I use Windows authentication in SQLCMD?

Connect to a SQL Server instance using Windows Authentication. Open the Command Prompt and switch to the location of the sqlcmd utility. Then, execute the following command by replacing the connection parameters with the server ( server_name ) and instance ( instance_name ) names to which you want to connect.

What is Setvar SQL?

The setvar command is used to define scripting variables. Variables that are defined by using the setvar command are stored internally. Scripting variables should not be confused with environment variables that are defined at the command prompt by using SET.

How can I tell if SQLCMD is enabled?

You could try to execute sqlcmd.exe -? in a process in your C# app - if it works, then SQLCMD is present - if not, it'll tell you something like "file not found" or "command invalid" or something ....

Does SQLCMD include SSMS?

SQLCMD is installed with SSMS and with the database engine. This means it's going to be installed on the server where your instance is located and probably your workstation as well.


1 Answers

The error message sounds like sqlcmd cannot find the .sql file.

Try sqlcmd "c:\example filepath\test.sql" from the command prompt.

Strings are quoted with single quotes in TSQL, with double quotes in cmd.

like image 142
devio Avatar answered Oct 19 '22 05:10

devio