solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Whitepapers SQL Server Tools SQL Server Webcasts SQL Server Questions and Answers SQL Server Questions and Answers













BCP to export data from global temp table

Asked 2/1/2012 12:26:48 PM by TonyM

Hi all,

I'm getting the following error:

SQLState = S0001, NativeError = 0
Error=[Microsoft]SQLServer Native Client 10.0]Unable to open BCP host data-file

For the following SQL code:

USE Runtime
GO
CREATE TABLE ##EnergyWeekReport(
 Substation nvarchar(50) NOT NULL,
 Monday  float NULL,
 Tuesday  float NULL,
 Wednesday float NULL,
 Thursday float NULL,
 Friday  float NULL,
 Saturday float NULL,
 Sunday  float NULL,
 This_Week float NULL,
 Last_Week float NULL);
 
DECLARE
@Mon float,
@Tue float,
@Wed float,
@Thu float,
@Fri float,
@Sat float,
@Sun float,
@Week float,
@preWeek float,
@dateIN datetime,
@Filename nvarchar(50),
@bcpcmd nvarchar(500);
-- Assign values
SET @dateIN = '2012-1-23';
SET @Mon = dbo.fnSEL_Monday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Tue = dbo.fnSEL_Tuesday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Wed = dbo.fnSEL_Wednesday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Thu = dbo.fnSEL_Thursday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Fri = dbo.fnSEL_Friday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Sat = dbo.fnSEL_Saturday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Sun = dbo.fnSEL_Sunday_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @Week = dbo.fnSEL_We***t_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
SET @preWeek = dbo.fnSEL_preWe***t_MWh(@dateIN, 'CB02_SS1_351S_1P7_MWh', 'CB02_SS2_351S_1P7_MWh');
INSERT INTO ##EnergyWeekReport
SELECT '138kV Line 1',@Mon, @Tue, @Wed, @Thu, @Fri, @Sat, @Sun, @Week, @preWeek;
SET @bcpcmd = 'bcp "SELECT * FROM ##EnergyWeekReport" queryout c\test.txt -c -T -S ww-historian';
EXEC xp_cmdshell @bcpcmd ;
DROP TABLE dbo.##EnergyWeekReport
GO

I'm running this in management studio on the machine where the database resides.  I am logged in as an 'aaAdministrator' with all the god rights needed.

Please advise



Answered 2/3/2012 7:53:18 AM by Scott C (1) vote answer is useful vote answer is NOT useful

When you use xp_cmdshell, the command runs under the SQL Server service account rather than using your login.  Apparently this account does not have rights to write to C:\.  Does it work if you run the bcp line in a Command Prompt window?


Answered 2/3/2012 8:56:39 AM by Jeremy Kadlec (0) vote answer is useful vote answer is NOT useful

Scott C,

Great suggestion!

Thank you,
Jeremy Kadlec


Answered 2/3/2012 9:57:08 AM by TonyM (0) vote answer is useful vote answer is NOT useful

Thanks for the responses.  FYI: my issue was caused by not having a colon (:) in the destination output file path "c\test.txt".  Ugh...


Answered 2/3/2012 10:02:32 AM by Jeremy Kadlec (1) vote answer is useful vote answer is NOT useful

TonyM,

That will do it every time...

Thank you for the follow-up to have the final answer.

Thank you,
Jeremy Kadlec



Post an Answer

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"SQL doctor ROCKS! As soon as I ran it, problems that have been giving me headaches were identified and cured."

SQL Monitor – For database professionals who need results on Day One. Try it online.

Need SQL Server help and not sure where to turn? Reach out to the Edgewood experts for a Health Check starting at $995.

Find and Fix SQL issues with Foglight Performance Analysis. Get a free copy.

Join the over million SQL Server Professionals who get their issues resolved daily.

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com