SQL Server Agent unable to run SQLCMD - sql

An SQL Server instance was set up without access to the Internet and failed to install SQLCMD during the initial SQL Server installation process. Upon discovering this SQLCMD was installed through the MsSqlCmdLnUtils.msi installer. Can now run SQLCMD through command line, as user account, but when running SQLCMD as part of xp_cmdshell the error is
'sqlcmd' is not recognized as an internal or external command,operable program or batch file.
I've manually given read/write access to all the SQLCmd files, i.e. .exe, .rll, etc, to the SQL Agent NT account. I can manually run the sqlcmd through command line as user account. Xp_cmdshell can run successfully when given other command line tasks, i.e. moving files, mapping drives, etc. Restarted SQL Server Agent.
SET #sqlCommand =
'sqlcmd -S hqconnectsql1 -d M -Q "SET NOCOUNT ON; select * from temp_travelSurvey" -o"' +
#filePath + #fileName +
'" -W -s"|" -w 1024'
EXEC master..xp_cmdshell #sqlCommand
GO
The above code is what is failing when run as a query through SSMS, left out are the variables that are declared and set prior to the command.
EXEC master..xp_cmdshell 'net use Y: \\HQPCISQL\e$ troperM12 /USER:aaa177\Reportsrv'
go
This code works successfully when run as a query through SSMS, showing that xp_cmdshell is not the issue.
The expected result, which I have been able to have succeed when run through the command prompt, is for a file containing the content of temp_travelSurvey to be created. Current error message is the sqlcmd is not a recognized internal or external command.

on SSMS you can execute your sql directly, you need not to use SQLCMD there. SQLCMD is command line interface to execute SQL from command prompt. I hope this would help you.
Like in above case directly execute sql scripts as below.
select * from temp_travelSurvey

Related

How to execute SQL script in directory from another script?

Say I have a .sql script stored in c:\scripts\some_script.sql.
What command do I use to execute this script from within another script or stored procedure.
For example
create procedure dbo.sp_execute_script
#script_location varchar(100)
as
execute(#script_location)
-- does not work
go
I am running the scripts in SSMS.
Use xp_cmdshell to run sqlcmd utility to execute your script:
exec xp_cmdshell 'SQLCMD -S <Server> -E -i "C:\path\script.sql"

How to execute generated script(.sql file) with schema and data in SQL Server 2008

Using SSMS 2008 I am able to generate a script for a database with huge amounts of data in file ABC.sql on my desktop.
The database has approx. 9 GB of data so I'm unable to open the file. Is there any way to execute the script?
When I try to open it in SSMS I get an error:
The operation could not be completed. not enough storage is available to complete this operation
The template specified cannot be found. Please check that the full path is correct
SQL Server offers 2 command prompt features that can se used for executing large queries - osql (will be removed in future), and sqlcmd
osql is located in the Tools\Binn subfolder. To execute a SQL script:
Start the Command Prompt
Navigate to the folder where the osql utility is located
Run the command in the following format:
osql –H <workstation name> -S <server_name[\instance_name]> -U <user login ID> -P <login password> –i <full path to script>
To execute the large.sql file located in the D:\test, against the Central database on the SQL Server instance Dell\SQL2012, as an sa with the 'sqladmin' password, run the following command:
osql -H Dell -S Dell\SQL2012 -i D:\test\large.sql -U sa -P sqladmin
The sqlcmd command line utility is also located in the SQL Server’s Tools\Binn sub-directory. To execute a SQL script:
Start the Command Prompt
Navigate to the folder where the sqlcmd utility is located
Run a command in the following format:
sqlcmd –S <server name> -d <database name> -i <full path to script> -U <user login ID> –P <login password>
To execute the same as above, run the following command:
sqlcmd -S Dell\SQL2012 -d Central -i D:\test\large.sql -U sa –P sqladmin
Start the sqlcmd Utility
Run Transact-SQL Script Files Using sqlcmd
I use sqlcmd to execute large SQL files.
You can generate script of your database by RightClick on your database Tasks>GenerateScripts> click next on Generate and Script window Check on select specific table choose tables you want Press next Click on Advance option on end of General Category select Type of data to script now choose which kind you want your database to.
Scheme Only: Means this script will create your database.
DataOnly:If you have created database and table this will insert data into it.
Press ok then Next.
Your file is by default save in
C:\Users[UserName]\Documents\ .

how to run tsql from a remote system in command prompt

I wanted to know how to run a TSQL statement from a remote system by connecting to the other system and run a TSQL statement through command prompt.
I have tried the following code but this code doesnt run on the other system.
PS: other system doesnt have sql server installed so it is compulsory that we use just command prompt to run from that system.
sqlcmd -S 100,1433\MSSQLSERVER -U sa -P abc -i C:\table.sql -o C:\output.txt
SQL Server 2000 - osql Utility
There's file called OSQL.exe which I am using for this job.
File comes from SQL Server 2000 (52 KB).
You can run it in CMD with simmiliar arguments to sqlcommand.
Remember to use GO after each batch
Example
Select * from Sales [enter]
GO [enter]

XP_CMDSHELL bcp error

I am executing the XP_CMDSHELL as below:
Exec XP_CMDSHELL 'bcp "Select OrderID, OrderDate, OrderDesc from DB.dbo.Order" queryout C:\Orderfile.txt -k -t \t -c -Slocalhost -T'
I got the following error:
"Unable to open BCP host datafile."
I looked at the c:\ drive and there is a file Orderfile.txt exist. ANd i have not opened the file or any other program doesn't not the file. But still I am getting this error.
Anyone have any idea why I am getting this error? Thank you in advance.
One of two issues:
XP_CMDSHELL uses the SQL Server service account credentials for filesystem (and other) access. The service account won't have permissions on the root of C: and it doesn't matter if you can see it
SQL Server is on a server somewhere (that is, isn't a local install) and you are trying to access your local C: drive. SQL Server can't see this of course...
there should be full permission on drive.
file is create automatically.
exec master..xp_cmdshell 'BCP "Select * From [test].[dbo].[Customer]" QUERYOUT C:\Backup\customer123.txt -S HP-PC -T -c'

How to figure out all the instances on sql server?

I am connected to one instance of the database server. How will i come to know if there are any other instances on the same server and their names?
First go to Start >> Run >> CMD (Open command prompt). Once in command prompt run following command based on SQL Server version installed on local machine.
For SQL Server 2000:
C:\> isql -L
For SQL Server 2005 / SQL Server 2008:
C:\> osql -L
OR
C:\> sqlcmd -L
As SQL script below is the snippet - Source
This script requires execute permissions on XP_CMDShell.
CREATE TABLE #servers(sname VARCHAR(255))
INSERT #servers (sname)
EXEC master..xp_CMDShell 'ISQL -L'
DELETE
FROM #servers
WHERE sname='Servers:'
OR sname IS NULL
SELECT LTRIM(sname)
FROM #servers
DROP TABLE #servers