Sqlcmd: Error: Syntax error at line 17 near command ':r ' in file - sqlcmd

I need to execute multiple sql files using a batch file. So, I created a file CREATE_DB.sql with following code:
/* SCRIPT: CREATE_DB.sql */
/* BUILD A DATABASE */
-- This is the main caller for each script
SET NOCOUNT ON
GO
PRINT 'CREATING DATABASE'
IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = 'MSSQLTIPS')
DROP DATABASE MSSQLTIPS
GO
CREATE DATABASE MSSQLTIPS
GO
:On Error exit
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_TABLES.sql
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\TABLE_INSERTS.sql
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_INDEXES.sql
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_PROCEDURES.sql
PRINT 'DATABASE CREATE IS COMPLETE'
GO
Then, I create a batch file create_db.bat with following code:
SQLCMD -E -dmaster -i"C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\create_db.sql"
PAUSE
But, on double-clicking the batch file it shows the following error:
Sqlcmd: Error: Syntax error at line 17 near command ':r ' in file 'C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\create_db.sql'.
This, I think, is due to the Spaces in the file paths, but how to get rid of this error?

Related

Failed to open file, Error 2 - MySQL Import

I'm trying to import a MySQL Database via command line (Terminal). The file's path seems to be correct, but I keep getting an error message that is most likely showing that, the path of the SQL file I'm importing is not correct. Below is the sample of the command line I'm using and the error I'm getting...
NOTE: Linux Server, Centos 7 - MariaDB
source C:/Users/localcomputerUser/SQLFileFolder/SQLFile.sql
source C:/Users/localcomputerUser/SQLFileFolder/SQLFile.sql
Failed to open file 'C:/Users/localcomputerUser/SQLFileFolder/SQLFile.sql
', error: 2
Thanks for your support.

Syntax error received when trying to source a file from mysql command line

I have a file that is filled with statements that appear like the following:
update table set column1=NULL where column2 in (select id from users where user_id in ('user1','user2'));
As you can see this is correct syntax. When I run this command manually on the mysql command line it works as intended.
However when I attempt to source the file I get syntax errors. Has anyone seen this issue before? I'm looking for a resolution...
mysql> \! source /tmp/update_users.sql
/tmp/update_users.sql: line 2: syntax error near unexpected token `('
/tmp/update_users.sql: line 2: `(select id from users where user_id in ('user1','user2'));'
You have to use the MySQL source command:
mysql> source /tmp/update_users.sql
When you use \!, you instead invoke a shell command. The shell source command expects a shell script, not a sql file.

Can't load a file to mysql table: Errcode: 2 - No such file or directory

I want to load file into a table. I am using this command.
LOAD DATA LOCAL INFILE 'home/myuser/Documents/my_project/project_sub_directory/project_sub_directory2/project_sub_directory3/my_data_file.txt'
INTO TABLE `mydatabse`.`my_table`
fields terminated BY ',';
I use LOCAL in the command to avoid the following error (an avoid moving my data files to another directory):
Error Code: 1290. The MySQL server is running with the
--secure-file-priv option so it cannot execute this statement
After executing the command, I get this error:
Error Code: 2. File
'home/myuser/Documents/my_project/project_sub_directory/project_sub_directory2/project_sub_directory3/my_data_file.txt'
not found (Errcode: 2 - No such file or directory)
How to resolve the issue without moving my data file to another directory?

How can I Import and export data using bcp command?

I am Trying to import and export data using bcp tool but it was giving errors which are:-
SQLState = 08001, NativeError = -1
Error = [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces:
Error Locating Server/Instance Specified [xFFFFFFFF].
SQLState = 08001, NativeError = -1
Error = [Microsoft][SQL Server Native Client 10.0]A network-related or instance-
specific error has occurred while establishing a connection to SQL Server. Serve
r is not found or not accessible. Check if instance name is correct and if SQL S
erver is configured to allow remote connections. For more information see SQL Se
rver Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Login timeout expired
I am currently using SQL server 2008 r2 version.
Command Which are I am using:-
C:\Program Files\Microsoft SQL Server\100\Tools\Binn>bcp Alldbtypes.dbo.Misc_dty
pes out C:\Workarea\Data\EmployeeData.dat -S 192.168.1.117\SqlSrv2008 -T
Please suggest me how can I use bcp command.
In order to user the bpc, it is important to know the schema of your database.
I believe the Export will look something like this:
bcp "Select Column1, Column2 FROM Database.Schema.Table" queryout
C:\bcp_outputQuery.txt -SYourServerName -T -c
OR something like this:
bcp DatabaseName.Schema.TableName out C:\Data\tableout.txt -c –T
For the import it would look something like this:
bcp Database.Schema.Table in
C:\bcp_outputQuery.txt -SYourServername -T -c

Errors during import of sql file to MySQL database

I am new to database and have just started to learn MYSQL. I have downloaded a sql file called rating.sql and would like to import it to the MYSQL database installed on the local machine. The operation system is Windows 7. The path for the sql file is:
"E:\rating.sql".
I am using the following commands in the MySQL Command Line Client
mysql>use database_name;
mysql>source E:\rating.sql;
The system gives the following error message:
ERROR 1049 <42000>: Unknown database 'ating.sql'
It is definitely something related to the path. Can anyone explain how this error is generated?Thanks
You have use \ and this is an escape character (omits the immediate following character. So to fix this you can use \ instead.
mysql>resour E:\\rating.sql;
Mysql recognises \r as an escape for a carriage return character.
To make it simpler, could you rename your file to something beginning with another letter...mustn't be b n r t or z...