Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error connecting to db (bcp)

Author  Topic 

jallah_88
Starting Member

12 Posts

Posted - 2011-02-10 : 04:49:30
Hi

I'm trying to export a sql file to a csv file using following code:

DECLARE @ExportCsv varchar(8000)

SET @ExportCsv = 'bcp SC2835128..testing out E:\Martin.Christiansen\FirewallRegler.csv -c -t, -T -S' + convert(varchar,@@ServerName)

PRINT @ExportCsv
EXEC master..xp_cmdshell @Exportcsv


When i try to execute this with inside ssms it gives me following error:
SQLState = 37000, NativeError = 4060
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open database "SC2835128" requested by the login. The login failed.
SQLState = 28000, NativeError = 18456
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'NT AUTHORITY\SYSTEM'.
NULL

What is wrong?

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-10 : 05:04:44
You are specifying the server name in the -T switch so there is no need for this to be in the database name (unless it's a linked server? is SC2835128 a linked server name?). Try this:

SET @ExportCsv = 'bcp testing out E:\Martin.Christiansen\FirewallRegler.csv -c -t, -T -S ' + convert(varchar,@@ServerName)

- Lumbago
My blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/
Go to Top of Page

jallah_88
Starting Member

12 Posts

Posted - 2011-02-10 : 05:14:07
Wow thanks, didnt know that. But now i get this error instead, which i really dont understand as testing is a functioning permanent table in my db?


SQLState = S0002, NativeError = 208
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 'testing'.
NULL


EDIT: fixed it. I must have slept when i read the many tutorials around the web. Working bcp is

'bcp Monitor..testing out E:\Martin.Christiansen\FirewallRegler.csv -c -t, -T -S' + convert(varchar,@@ServerName)

Of course i needed to specify the db to connect to
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-10 : 05:26:01
Excellent...I should of course thought of this as well, but my excuse is that I don't use bcp very much

- Lumbago
My blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/
Go to Top of Page
   

- Advertisement -