How can I Import and export data using bcp command? - sql-server-2008

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

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.

sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user

Trying to connect to MSSQL DB with pyodbc and SQLAlchemy using code (from another SO post) like
import json
import urllib
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
CONF = json.load(open(f"{PROJECT_HOME}/configs/configs.json"))
assert isinstance(CONF, ty.Dict)
params = urllib.parse.quote_plus(f"DRIVER={{ODBC Driver 17 for SQL Server}};"
f"SERVER={CONF['db']['db_ip']};"
f"DATABASE={CONF['db']['dest_db']};"
f"UID={CONF['db']['username']};"
f"PWD={CONF['db']['password']}")
engine = create_engine(f"mssql+pyodbc:///?odbc_connect={params}")
print(params)
sql_conn = engine.raw_connection()
cursor = sql_conn.cursor()
and getting error
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'myuser'. (18456) (SQLDriverConnect)")
Looking at the URL that gets generated, it looks like
mssql+pyodbc:///?odbc_connect=DRIVER%3D%7BODBC+Driver+17+for+SQL+Server%7D%3BSERVER%3D172.12.3.45%3BDATABASE%3Dmy_db%3BUID%3Dmyuser%3BPWD%3Dpass%2Bword10
even though 1) I can connect to the same DB via mssql-tools (eg. via bcp) using the same credentials I'm using here and 2) this same code snippet does work for another script that accesses a different DB (though was a slightly older version of SQLAlchemy (v1.3.15), but same ODBC DSN).
Any ideas what could be going wrong in this case?
The configs I'm using in the code above look like
{
"db":
{
"db_dsn": "MyMSSQLServer",
"db_ip": "172.12.3.45",
"dest_db": "my_db",
"dest_table": "my_table",
"username": "myuser",
"password": "pass+word10"
}
}
Note that my password here is "pass+word10" w/ a plus sign. When I look at the docs for urllib.parse.quote_plus, it seems like the plus sign should not be an issue, but I may be misinterpreting that and I can't think of what else it could be (since, again this general snippet has already worked in the past).
My various package versions are...
(venv) $ pip freeze | grep odbc
pyodbc==4.0.30
(venv) $ pip freeze | grep SQL
Flask-SQLAlchemy==2.5.1
SQLAlchemy==1.4.7
SQLAlchemy-JSONField==1.0.0
SQLAlchemy-Utils==0.37.0
Have also tried
engine = create_engine("mssql+pyodbc:///?odbc_connect="
"DRIVER={ODBC Driver 17 for SQL Server};"
f"SERVER={CONF['db']['db_ip']};"
f"DATABASE={CONF['db']['dest_db']};"
f"UID={CONF['db']['username']};"
f"PWD={CONF['db']['password']}")
(w/ and w/out "+"s between the driver words) showing the engine string as
Engine(mssql+pyodbc:///?DATABASE=mydb&PWD=mypassword&SERVER=172.12.3.45&UID=myuser&odbc_connect=DRIVER%3D%7BODBC+Driver+17+for+SQL+Server%7D)
and gotten the error
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')
Even though I can see a DSN in my odbc.ini like...
$cat /etc/odbc.ini
[MyMSSQLServer]
Driver=ODBC Driver 17 for SQL Server
Description=My MS SQL Server
Trace=No
Server=172.12.3.45
Anyone with more experience with this know what could be going on here? Any debugging stuff I could try or info that could help this post?
* I know this post title is not super informative for what the problem seems to be, but I don't really have a great idea of what the problem is even related to atm, so would need to get more info on the problem before being able to edit to make a more relevant title.

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

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?

What working directory do I have to be in to connect to a MySQL server in R

I am using a Windows 7 64-bit computer, trying to connect to an ODBC driver directly with R. I tried downloading the RMySQL package as a source, but for some reason it won't install and just leaves error messages like this:
* installing *source* package 'RMySQL' ...
** package 'RMySQL' successfully unpacked and MD5 sums checked
Warning: running command 'sh ./configure.win' had status 127
ERROR: configuration failed for package 'RMySQL'
* removing 'C:/Users/jizzard/Documents/R/win-library/3.1/RMySQL'
Warning in install.packages :
running command '"C:/PROGRA~1/R/R-31~1.1/bin/x64/R" CMD INSTALL -l "C:\Users\jizzard\Documents\R\win-library\3.1" C:\Users\jizzard\AppData\Local\Temp\Rtmp8qnScH/downloaded_packages/RMySQL_0.9-3.tar.gz' had status 1
Warning in install.packages :
installation of package ‘RMySQL’ had non-zero exit status
So I'm trying to use the RODBC package instead. I've got my database as an ODBC driver shortcut (Verizon DMP)
but when I try to type con = odbcConnect("Verizon Fios") in R, no connection is established. What is going wrong?
odbcConnect() requires your username (uid) and password (pwd):
odbcConnect(dsn, uid, pwd, ...)

R ODBC MySQL Connection Example

I'm trying to use RODBC to connect to a MySQL db on my computer (I'm assuming it's localhost). I've read the package reference manual and can't figure out how to do anything (connect, set default driver, open channel, etc). Any suggestions?
EDIT:
> install.packages("RMySQL", type="source")
Installing package(s) into ‘C:/Users/backupSam/Documents/R/win-library/2.13’
(as ‘lib’ is unspecified)
trying URL 'http://lib.stat.cmu.edu/R/CRAN/src/contrib/RMySQL_0.8-0.tar.gz'
Content type 'application/x-gzip' length 160735 bytes (156 Kb)
opened URL
downloaded 156 Kb
* installing *source* package 'RMySQL' ...
ERROR: configuration failed for package 'RMySQL'
* removing 'C:/Users/backupSam/Documents/R/win-library/2.13/RMySQL'
* restoring previous 'C:/Users/backupSam/Documents/R/win-library/2.13/RMySQL'
The downloaded packages are in
‘C:\Users\backupSam\AppData\Local\Temp\RtmpitXEFu\downloaded_packages’
Warning messages:
1: running command 'C:/PROGRA~1/R/R-213~1.2/bin/x64/R CMD INSTALL -l "C:/Users/backupSam/Documents/R/win-library/2.13" C:\Users\BACKUP~1\AppData\Local\Temp\RtmpitXEFu/downloaded_packages/RMySQL_0.8-0.tar.gz' had status 1
2: In install.packages("RMySQL", type = "source") :
installation of package 'RMySQL' had non-zero exit status
First set up a connection.
1) For me I had to download a driver on MySql's website, which will vary by system and version, I used this page:
Windows ODBC Drivers
2) Once this is downloaded run the setup utility.
3) Next setup up the DSN. Instructions for windows are here: MySQL ODBC DSN Setup
4) Important: Remember the name of the DSN as it is used when you create the channel in RODBC to connect to your database.
5) Finally, once this is setup you install and load the RODBC package.
6) To connect to your database use something like this:
channel <- odbcConnect("mysql 2", uid="root")
where 'mysql 2' is the name of your DSN connection, NOT the name of the database.
7) Finally you can send a query like this:
result1 <- sqlQuery(channel, paste("SELECT * from db1"))