sqlcmd using windows authentication in automation script - batch-file

I am using below command in bat file to get some information from the list of sql instance,i am using -E to login as windows authentication when i manually triggering the bat file.
Could anyone help how to configure it on SQLJOB.i am sure that - E will not work on SQLjob ..
Thanks in advance!!
FOR /F %%G IN (E:\serverlist\serverlist.txt) DO (echo %%G
sqlcmd -S %%G -iE:\serverlist\some.sql -h-1 -W -w1000 -s "," -m1 -E >>E:\output\result.txt
)

using proxy account helped to resolve the issue.just add the sqlcmd command under a job step with Run as 'Proxy account'

try this.
sqlcmd -S .\sqlexpress -E -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a local SQLExpress server using Windows Authentication)
sqlcmd -S mysql -U sqluser -P sqlpassword -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a SQL server using SQL authentication)

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

Psexec to execute batch file,which calls a vbscript,and this vbscript executes test case using qtp on a remote machine

Scenario :
System 1(Remote Machine) :
"abc.bat"
"xyz.vbs"
Both the files are in D Drive
Scenario : "abc.bat" calls "xyz.vbs" and the command is like this: cscript D:\xyz.vbs
"xyz.vbs has script which calls QTP and executes the test script on QTP.
System 2(My Machine) :
I have downloaded PSTools and making use of Psexec command to execute batch file on above remote machine.
In my cmd i have this command.
C:\Program Files\PSTools>psexec -i \System_1's_IP_address -u domain\username -p password D:\abc.bat
When i run this command on my machine, it starts PSexec on remote machine. But QTP is not invoked on the remote machine so execution halts here.
I have checked in task manager.it shows wscript,psexec.
But not QTPro.exe
Is there a solution??
Can somebody please help.
PSEXEC \\<Targetsystem> -u domain\username -p password -i -w D: cmd.exe /c abc.bat
or
place the abc.bat in the same folder as psexec and execute this one:
PSEXEC \\<Targetsystem> -u domain\username -p password -i -c -f abc.bat
(P.S.: a look at PSEXEC /? may help ;))

Running bulk of SQL Scripts in Sybase through batch

I was using below code(as .bat file) to recursively execute the bulk of .sql files, having SQL SERVER 2008 R2 as backend:
for /R %%G in (*.sql) do sqlcmd /S [Database Server] /d [Database name] -U [Username] - P[Password] -i"%%G"
pause
Now, i i have to execute bulk of sql scripts but this time Sybase as backend.
Please suggest me what modification should i do to make it run for 'Sybase'!!
The connection string for Sybase is very similar
isql -U [username] -P [password] -S [servername] -D [dbname] -i [scriptname]
So your script will look something like:
for /R %%G in (*.sql) do isql -S ServerName -D DbName -U Username -P Password -i"%%G"
pause
It should require minimal changes to get it to work on Sybase vs SQLServer.

Resources