Running batch with BCP commands via SQL Server Agent - sql-server

When I run batch file with:
BCP "SELECT * FROM [KW_GRECOS].[dbo].[V_G076a]" queryout "G:\Bulk\V_G076a_new.bcp" -N -S localhost -T -E
BCP "[KW_GRECOS].[dbo].[hist_V_G076a]" IN "G:\Bulk\V_G076a_new.bcp" -N -S localhost -T -E -b 1000000 -h "CHECK_CONSTRAINTS"
It works perfectly, but when I execute same batch from job in SQL Server Agent:
cmd.exe /c "E:\batch\Bulk_copy.bat"
It's not working:
Date,Source,Severity,Step ID,Server,Job Name,Step
Name,Notifications,Message,Duration,Sql Severity,Sql Message
ID,Operator Emailed,Operator Net sent,Operator Paged,Retries Attempted
12/21/2017 00:40:37,NightBulk,Error,,DBSERVER,NightBulk,,,The job
failed. The Job was invoked by User eTour\ITcom. The last step to
run was step 1 (1 Bulk insert). The job was requested to start at
step 1 (1 Bulk insert).,00:00:00,0,0,,,,0 12/21/2017
00:40:37,NightBulk,Error,1,DBSERVER,NightBulk,1 Bulk insert,,Executed
as user: DBSERVER\ITcom. C:\Windows\system32>BCP "SELECT * FROM
[KW_eTour].[dbo].[V_G075a]" queryout "G:\Bulk\V_G075a_new.bcp" -N -S
localhost -T -E SQLState = S1000 NativeError = 0 Error =
[Microsoft][ODBC Driver 11 for SQL Server]Unable to open BCP host
data-file. Process Exit Code 1. The step failed.,00:00:00,0,0,,,,0

It is due to insufficient permissions.
The SQL Server Agent service-user can not access the file G:\Bulk\V_G075a_new.bcp.
You have the following options:
Move the file to a location where the service-user can access it.
Modify the NTFS-permissions of the file/folder to allow the SQL Server Agent service-user to access the file

Related

PsExec command working on cmd doesn't work inside .bat file nor via SQL xp_cmdshell

When I execute the following command on machine1's cmd it works fine:
C:\PSTools\psexec64.exe \\machine2 -u domain\user -p pass -i -d -accepteula C:\folder\file.bat
However, if I try to run it via xp_cmdshell (sql server is also on machine1) as bellow it will fail:
exec xp_cmdshell 'C:\PSTools\psexec64.exe \\machine2 -u domain\user -p pass -i -d -accepteula C:\folder\file.bat'
Here is the output:
NULL
PsExec v2.4 - Execute processes remotely
Copyright (C) 2001-2022 Mark Russinovich
Sysinternals - www.sysinternals.com
NULL
Connecting to xx.xxx.xxx.xx... Starting PSEXESVC service on xx.xxx.xxx.xx... Could not start PSEXESVC service on xx.xxx.xxx.xx:
Access is denied.
Connecting to xx.xxx.xxx.xx... Starting PSEXESVC service on xx.xxx.xxx.xx...
I also tried to save the command as a bat file to see if the problem is exclusive to the SQL server, but it won't work either.
Here is the output:
PsExec could not start C:\folder\file.bat on xx.xxx.xxx.xx:
The user name or password is incorrect.
The exact same command works fine on cmd, same machine.

Why is sqlcmd not importing anything?

I'm trying to restore a 500MB .sql file to SQL Server; I've tried running the script directly from a query page, but the memory isn't enough, so I tried with the sqlcmd command as follows:
sqlcmd -S <my server name> -d <my dbname>-U <user> -P <password> -i <pathfile> -a 32767
I've also tried with the -o command, too see if the log would report something useful, but from that command I didn't get any error.
Still, my database in SQL Server is empty, without any single table.
I'm running this on a Windows 10 system.
Any suggestions?
If you are using a SQL instance then the syntax will be like this:
sqlcmd -S <my server name>\instance -d <my dbname> -U <user> -P <password> -i <pathfile> -a 32767

Run sql script on remote SQL Server database from my Linux machine

I have a .sql file in my linux machine. I would like to connect to MS SQL database in the remote windows machine and run the .sql file in that database.
osql -S servername -U xx -P yy runs okay and returns a SQL window like:
SQL>
I can run individual queries using this. I have a .sql file with lot of sql commands which I need to run on a database called abc. How can I do that using shell command. The following doesn't work for me,
osql -S mssql -U xx -P yy -i /home/admin/Script.sql -D abc
I get the following error,
Illegal option -i
Syntax: osql -S server -U user -P password
The path of Script.sql file is correct as this opens the file - vi /home/admin/Script.sql. Not sure what the problem is. Any help would be appreciated
osql isn't working for me. I tried sqlcmd and it worked like a charm. Referred these links for ms sql tools installation - http://www.thesqlreport.com/?p=1494, https://sqlserveronlinuxbackup.com/sqlcmd-command-not-found-ubuntu/.
sqlcmd syntax:
sqlcmd -S 10.0.0.0 -U xx -P yy -d mydb -i /home/admin/abc.sql

Executing large script file (1.7GB) on Microsoft SQL Server using SQLCMD

I have a generated script file with 1.7 GB size, I'm trying to run this script using the following sqlcmd
sqlcmd -m1 -S 192.168.100.10\SQLHQ -U sa -P 123456 -i "E:/VMS2008R2.sql"
When I run this line in CMD as administrator, the command take some time and give me no error, the database schema generated to database but without data and without any success or finished message!
UPDATE
When I run the command without -m1 I got the following exception:
TCP Provider: An existing connection was forcibly closed by the remote host.
Communication link failure

How do you execute large SQL Server script file?

I am trying to execute a large SQL Server script file from the command prompt as its not loading in Management Studio. I am using this command
sqlcmd -S .\SQLEXPRESS -U ttandel -P ''
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
Note: password is blank. I have tried all the options for keeping password as blank such as ("",'', ) but nothing is working.
Can anyone please suggest how to do this?
From comment:
I tried with this
sqlcmd -S .\SQLEXPRESS -E
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
This is throwing an error
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'NAPG\ttandel'..
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Cannot open database "[Zen.Databases.Suite]" requested by the login. The login failed..
My Server Security information as below.
The following SQL command will do the work.I have used it multiple times for installing 100 million to 200 million records in DB.
sqlcmd -S Krishneil-PC -E -i C:\Users\Krishneil\Desktop\Script.sql
change Krishneil to your suitability.
Change -d [Zen.Databases.Suite] to -d "Zen.Databases.Suite"

Resources