SQL2000 中的 BCP 输出错误:SQLState = 37000,NativeError = 4060

发布于 2024-07-07 19:01:27 字数 1352 浏览 10 评论 0原文

我创建了一个过程来获取我的机器上本地数据库中的所有用户表。 我希望能够使用 BCP 和 SQL 创建所有表的平面文件。 它是 SQL 2000 中的虚拟数据库,通过 Windows 身份验证进行连接。 我已经在 WinXP SP2 中设置了环境路径变量。 我已经创建了新用户来访问数据库,关闭了防火墙,使用可信连接。 我已经尝试了几十个论坛,但没有运气。

在 dos 命令提示符下我得到同样的错误。

SQL状态 = 37000,本机错误 = 4060 错误 = [Microsoft][ODBC SQL Server 驱动程序][SQL Server]无法打开登录“[HelpDesk-EasyPay”中请求的数据库。 登录失败。

这是我的 SP:

@Path VARCHAR(100),
    @UserName VARCHAR(15),
    @PassWord VARCHAR(15),
    @ServerName VARCHAR(15)
AS 

set quoted_identifier off
set nocount on

declare @n int
declare @db varchar(40)
set @db=DB_NAME()
declare @TableName varchar(15) 
declare @bcp varchar(200)
select identity(int,1,1) as tblNo,name tblname into #T from Sysobjects where xtype='u'
select @n=COUNT(*) from #T

WHILE (@n>0)
BEGIN
    SELECT @TableName=tblname FROM #T WHERE tblno=@n
    PRINT 'Now BCP out for table: ' + @TableName
    SET @bcp = "master..xp_cmdshell 'BCP " + "[" + @db + ".." + @TableName + "]" + " OUT" + @Path + "" + @TableName+".txt -c -U" + @UserName + " -P" + @PassWord + " -S" + @ServerName + " -T" + "'" 
    EXEC(@bcp)
    SET @n=@n-1
END

DROP TABLE #T

任何人都可以提供建议吗? 这似乎是连接问题或 BCP 问题? 没有把握。

编辑:我从查询分析器运行这个,因为我有 118 个表要输出到平面文件。 我似乎同意这是一个身份验证问题,因为我尝试使用用户名 sa 密码 root 连接到主数据库。 这就是它的设置,我得到相同的错误:SQLState = 37000,NativeError = 4060

I have created a proc that grabs all the user tables in a local DB on my machine. I want to be able to create a flat file of all my tables using BCP and SQL. Its a dummy database in SQL 2000 connecting through windows authentication. I have set my enviroment path variable in WinXP SP2. I have created new users to access the db, switched off my firewall, using trusted connection. I have tried dozens of forums, no luck.

In dos command prompt I get the same error.

SQLState = 37000, NativeError = 4060
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database requested in login '[HelpDesk-EasyPay'. Login fails.

Here is my SP:

@Path VARCHAR(100),
    @UserName VARCHAR(15),
    @PassWord VARCHAR(15),
    @ServerName VARCHAR(15)
AS 

set quoted_identifier off
set nocount on

declare @n int
declare @db varchar(40)
set @db=DB_NAME()
declare @TableName varchar(15) 
declare @bcp varchar(200)
select identity(int,1,1) as tblNo,name tblname into #T from Sysobjects where xtype='u'
select @n=COUNT(*) from #T

WHILE (@n>0)
BEGIN
    SELECT @TableName=tblname FROM #T WHERE tblno=@n
    PRINT 'Now BCP out for table: ' + @TableName
    SET @bcp = "master..xp_cmdshell 'BCP " + "[" + @db + ".." + @TableName + "]" + " OUT" + @Path + "" + @TableName+".txt -c -U" + @UserName + " -P" + @PassWord + " -S" + @ServerName + " -T" + "'" 
    EXEC(@bcp)
    SET @n=@n-1
END

DROP TABLE #T

Can anyone advise. This seems to be a connection problem or BCP ? Not sure.

edit: I am running this from query analyzer because I have 118 tables to output to flat file. I seem to agree that its an authentication issue because I tried connecting to master db with username sa password root. which is what its set to and I get the same error: SQLState = 37000, NativeError = 4060

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思碎 2024-07-14 19:01:27

您的括号延伸到整个限定表名称 - 只有各个组件应该用括号括起来:

bcp [HelpDesk-EasyPay].dbo.[customer] out d:\MSSQL\Data\customer.bcp -N -Utest -Ptest -T

应该可以工作,所以您希望:

SET @bcp = "master..xp_cmdshell 'BCP " + "[" + @db + "]..[" + @TableName + "]" + " OUT" + @Path + "" + @TableName+".txt -c -U" + @UserName + " -P" + @PassWord + " -S" + @ServerName + " -T" + "'" 

在您的代码中。 看起来您给出的错误消息已被截断,否则您将能够看到它正在尝试打开数据库“[HelpDesk-EasyPay.dbo.customer]”,当然,该数据库不存在,即使它存在这样做,然后您会收到一个错误,表明未指定表。

Your brackets are extending over the entire qualified table name - only the individual components should be bracketed:

bcp [HelpDesk-EasyPay].dbo.[customer] out d:\MSSQL\Data\customer.bcp -N -Utest -Ptest -T

should work, so you want:

SET @bcp = "master..xp_cmdshell 'BCP " + "[" + @db + "]..[" + @TableName + "]" + " OUT" + @Path + "" + @TableName+".txt -c -U" + @UserName + " -P" + @PassWord + " -S" + @ServerName + " -T" + "'" 

in your code. It looks like the error message you gave was truncated, otherwise you would have been able to see that it was attempting to open database "[HelpDesk-EasyPay.dbo.customer]" which, of course, does not exist, and even if it did, you would then get an error that no table was specified.

£冰雨忧蓝° 2024-07-14 19:01:27

我对 OUT 有同样的问题(减号字符会杀死所有事件,而 ^ 不起作用)

我用 QUERYOUT 来避免它。 像这样 :

SET  @s = 'BCP "SELECT * FROM [HelpDesk-EasyPay].dbo.customers" QUERYOUT myfile.txt ...'

I have the same issue for the OUT (the minus character kills everything event the ^ don't work)

I avoid it with the QUERYOUT. Like this :

SET  @s = 'BCP "SELECT * FROM [HelpDesk-EasyPay].dbo.customers" QUERYOUT myfile.txt ...'
清旖 2024-07-14 19:01:27

也许有人会派上用场:我对 SQL Server 2017 也有同样的问题。原因是数据库名称周围的方括号。

Maybe someone will come in handy: I have the same issue with SQL Server 2017. The reason was the square brackets around the database name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文