BCP使用有效的声明,但传递的参数无效
我不得不使用BCP命令行工具将数据从SQL服务器数据库导出到一个文件中的一个红色的时候它抛出的错误时,既不给出的结果也没有输出任何东西帽子服务器。 我(显然)使用有效的陈述,但bcp没有产生任何类型的输出/结果。 但是,当我执行缺少或无效参数的语句时,它会显示相应的错误。 我正在查找此问题的原因(例如安装有缺陷,使用不当,bcp,缺少权限或任何其他已知冲突)以及如何解决此问题。BCP使用有效的声明,但传递的参数无效
BCP声明:
bcp fully_qualified_table_name out ./data.txt -c -S server -U user -P password
BCP用法:
usage: /opt/microsoft/bin/bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-q quoted identifier]
[-t field terminator] [-r row terminator]
[-a packetsize] [-K application intent]
[-S server name or DSN if -D provided] [-D treat -S as DSN]
[-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-d database name]
BCP版本:
BCP - Bulk Copy Program for Microsoft SQL Server.
Copyright (C) Microsoft Corporation. All Rights Reserved.
Version: 11.0.2270.0
SQL Server版本(SELECT @@VERSION
):
Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
May 14 2014 18:34:29
Copyright (c) Microsoft Corporation
Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600:) (Hypervisor)
分布:
Red Hat Enterprise Linux 6.7 (KornShell).
与相应的错误信息(示例)无效语句。
bcp THAT_TUB_ACE.oh_nerd.table_name out ./data.txt -c -S sr._bear -U you_sr. -P pass_sword
SQLState = S1T00, NativeError = 0
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
SQLState = 08001, NativeError = 11001
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = 08001, NativeError = 11001
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: Error code 0x2AF9
...
bcp fully_qualified_table_name ./data.txt -c -S valid_server -U valid_user -P bad_word
bcp fully_qualified_table_name out ./data.txt -c -S valid_server -U valid_user -P bad_word
SQLState = 28000, NativeError = 18456
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'valid_user'.
摘要。
的目标是使用下面的语法(或类似)来生成数据文件:
bcp fully_qualified_table_name out ./data.txt -c -S server -U user -P password
事实是:
- 当运行一个有效BCP声明没有什么窗口根本没有输出,也没有创建数据文件。
- 我不能为BCP使用(使用集成的安全可信的连接)选项
-T
所以我必须指定服务器,用户名和密码。 - 尝试
queryout
选项上一个非常简单的小桌子已经但仍然没有运气。 - 凭证有效,我使用
sqlcmd
成功对其进行了测试,如下所示:sqlcmd -S server -U user -P password -Q 'SELECT * FROM really_small_table'
。 - 的BCP下“无效的声明与相应的错误信息(例子)”这个问题部分陈述只是无效声明的例子表明,BCP实际上做的东西,但给出预期的结果。
希望你已经解决了你的问题,但是我在配置PyODBC for Python 3.4在RedHat 7.1上遇到类似的问题。我试图连接到Microsoft SQL Server 2016.希望这会帮助别人。
对我来说真正的问题是ODBC/FreeTDS配置。以下是我采取的安装步骤和最终的解决方案。
- 我开始安装Microsoft's ODBC driver for redhat。这可能不是必需的,但是我在这里列出它,因为我害怕破坏ODBC,所以我从来没有从我的Redhat机器中删除它。
- 安装ODBC和ODBC开发文件:
yum install unixODBC-devel.x86_64
- 安装freetds的:
yum install freetds.x86_64
- 更新配置文件。步骤1-3花了大约10分钟。我花了一个非常沮丧的一天尝试各种配置,通常我可以通过
tsql
(FreeTDS调试工具,下面更多)连接,但从来没有通过Python连接。我最终偶然发现了this blog,它通过ldconfig -p | grep libtdsodbc
指出我正确的驱动程序(我相信这指向FreeTDS驱动程序,因此可能不需要微软的驱动程序)。
最终的配置我结束了如下:
/etc/odbc.ini(注:我要创建这个文件作为ODBC/freetds的安装过程中没有创建它对我来说)。
[SQLServer]
Description = TDS driver (Sybase/MS SQL)
Driver = SQLServer
Servername = your_sql_hostname
TDS Version = 0.95
Database = your_database_name (not to be confused with instance name)
Port = 1433
/etc/odbcinst.ini
[ODBC]
# Enables ODBC debugging output. Helpful to see where things stop working.
Trace = yes
TraceFile = /etc/odbcinst.trace
[SQLServer]
Description=TDS driver (Sybase/MS SQL)
Driver=/lib64/libtdsodbc.so.0
Driver64=/lib64/libtdsodbc.so.0
UsageCount=1
故障排除提示
tsql是FreeTDS自带的调试/测试工具。这是你的朋友。我能够在早期使用它来验证网络方面的事情是否正确(即没有防火墙阻止访问,主机名解析等)。使用tsql -H your_sql_hostname -L
可以从SQL服务器获取实例名称和端口。请注意,您将在上面的ODBC配置中使用端口1433(这是MS SQL实例发现端口),而不是这些端口号。
ServerName your_sql_hostname
InstanceName instance_1_name
IsClustered No
Version 13.0.x.x
tcp 5555
ServerName your_sql_hostname
InstanceName instance_2_name
IsClustered No
Version 13.0.x.x
tcp 6666
从以前的T-SQL命令使用的端口,像这样:
tsql -H your_sql_hostname -p your_sql_instance_port -D your_database_name -U your_username -P your_password
你应该得到一个提示。尝试从数据库查询一些数据(请注意,您必须在单独的一行中键入GO
以实际运行您的查询)。如果这可行,但仍然无法通过Python/other-app连接,通常意味着ODBC/FreeTDS安装正确,并且没有网络问题,但是您的ODBC配置不正确。
1> SELECT TOP 1 * FROM your_table
2> GO
{a row will be returned here}
1> # prompt returns, use `exit` to get out
您的用户是否有写入权限的文件夹要写输出文件?由于BCP先尝试连接到SQL服务器,因此获取错误凭证的错误属于正常情况,所以如果失败,它将不会尝试执行其他任何操作。 –
嗨@IonutTulai,权限是正确的。 –