Share via


SQLCMD issues

Question

Monday, February 15, 2016 10:11 AM

EXEC xp_cmdshell  'sqlcmd -S servername-U sa -P password -d DBCoaching -i D:\SourceControl\SalesCoachingProduct\NetAppCoac\tcRepDetails2015.sql';

When i run this, it shows

qlcmd: Error: Error occurred while opening or operating on file (Reason: The device is not ready).

This query was working before. I am nt sure what happens now

But if i try to run a query direclty it works fine

EXEC xp_cmdshell  'sqlcmd -S servername-U sa -P password -d DBCoaching -q "select * from newhirelist"';

All replies (10)

Monday, February 15, 2016 10:38 AM ✅Answered

Yes, created  a shared folder to be recognized by MS SQL account and issue 

EXEC xp_cmdshell  'sqlcmd -S servername-U sa -P password -d DBCoaching -i \Yourcomp\sharedfolder\tcRepDetails2015.sql';

Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence


Monday, February 15, 2016 10:13 AM

Hi Catherine,

Could you please make sure that the File and directory structure existing on the server it self otherwise you could use UNC Path. If you use UNC path then make sure SQL Service account can have permissions on the folder.

D:\SourceControl\SalesCoachingProduct\NetAppCoac\tcRepDetails2015.sql'

Regards,

Balwant.

Failure in Life is failure to try... PGDCA-98(South Gujarat University),MCSA (SQL 2012) , MCTS (SQL Server 2005) http://blog.vspatel.co.uk/


Monday, February 15, 2016 10:16 AM

Looks like the path  you specified is for your local machine, please put the file to the server...

Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence


Monday, February 15, 2016 10:28 AM

Ya folder name i have mentioned is there in local not on server


Monday, February 15, 2016 10:34 AM

Is there any way to run the script from local machine?


Monday, February 15, 2016 10:37 AM

Is there any way to run the script from local machine?

Yes by specifying UNC path

ie as //MachineName/....

Also you the account executing xp_cmdshell should have access to folder you created in local machine

Alternatively you can create a proxy account for this purpose and give it required access

see

https://www.mssqltips.com/sqlservertip/2143/creating-a-sql-server-proxy-account-to-run-xpcmdshell/

Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh

My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page


Monday, February 15, 2016 10:46 AM

is this correct

//machineName/D:/SourceControl/SalesCoachingProduct/NetAppCoac/tcRepDetails2015.sql


Monday, February 15, 2016 10:52 AM

No

Log in on the server and run the below path. Does it open the folder on your machine?

//machineName/D$/SourceControl/SalesCoachingProduct/NetAppCoac

Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence


Monday, February 15, 2016 10:55 AM

is this correct

//machineName/D:/SourceControl/SalesCoachingProduct/NetAppCoac/tcRepDetails2015.sql

nope

right click on the folder and enable sharing and it will show shared path

copy and paste it for specifying path in xp_cmdshell command

Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh

My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page


Monday, February 15, 2016 11:03 AM

Hi CatherineJoy,

Try to open the path in run like i am opening my documents.

Where 

DIN16001023 is my hostname and mssql-2014 is my shared folder.

You need to share the folder and give R/W access to it.

Please mark me as answer if my post helps you.

Regards

ChetanV