Can we run sqlcmd from RDS instance? - batch-file

We have an EC2 instance for a .NET application and a RDS instance for SQL Server Express. Since Express doesn't support Sql Agent Jobs we need to use sqlcmd and schedule it as a batch. Can the batch file be setup in the RDS instance and if so how? If not, are there other free alternatives without setting up the SQL server in EC2?

You actually cannot RDP into an RDS instance:
From Microsoft SQL Server on Amazon RDS
In order to deliver a managed service experience, Amazon RDS does not
provide shell access to DB instances
So you cannot run sqlcmd directly on the instance. Likewise for batch files, they cannot be installed on the RDS instance.
As #vmachan suggested, you will need to install the client tools on a separate instance in order to access command line functionality.

You can use the command below. It works for me.
"c:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S
"xxxxxxxxx.us-east-1.rds.amazonaws.com" -d database -U user_id -P
password -i "path\SqlQueryName.sql"
or without database name
"c:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S
"xxxxxxxxx.us-east-1.rds.amazonaws.com" -U user_id -P password -i
"path\SqlQueryName.sql"

I think you might have to use some PowerShell commands from your EC2 instance for .Net app against the RDS instance..
A sample command would like below
C:\Users\Administrator> Invoke-Sqlcmd –ServerInstance -Database EduData –Query “SELECT id, name, value, testscore FROM TestScores” –Username -Password
NOTE: I think you would need the SQL Client software installed on your .Net app EC2 instance to be able to do this.
Now you can create your batch jobs on your EC2 instance.
Hope this helps

You cannot call CMD file from RDS instance. Unlike with on Prem sql servers where cmd can be called from a sql server agent job, in RDS it’s not possible. I had a task of stopping DMS task in sql server agent job and then do a fresh restore of the database using backup. This task of stopping DMS wanted to have it in sql agent job, tried a lot & the only solution I see was having this DMS task in a batch file outside the sql agent job and then running it first and then sql agent job to do the restore. Any other useful tips or solutions will be appreciated! Thanks! Note : it’s RDS and not sql on EC2.

Related

Is there a PowerShell function to reconnect to a SQL Server after restart?

I have a basic Microsoft SQL Server (SQL Server 2017 on Server 2016 Datacenter) in Azure that's currently being power-managed. Every morning I RDP into the VM, enter the SQL Server Management Studio, and reconnect using my local admin account.
The machines automatically reboot, but is there a way to automate the reconnection process?
I reconnect using a GUI interface with the fields:
Server Type: Database Engine
Server Name: hostname
Authentication: Windows Authentication
User name: localadmin
Password:
I then click connect and boom. It seems like there should be a function, something like:
Reconnect-SQLServer -Type DatabaseEngine -Name hostname
-Authentication Windows -Credentials $Credentials
Is there? I haven't been able to find one. Bear with me, I'm fairly new to this sort of stuff.
Edit: It was my impatience. After a short period of time, the database was online and automatically reconnected. The manual process was not needed. Thanks to those who tried to help out.
You can automate connecting sql server management studio with SSMS.exe flags like:
# use current running credentials for windows authentication
SSMS.exe -E -S hostname -d MyDatabase -nosplash
# use specific user account (will prompt for password)
SSMS.exe -U MyUserName -S hostname -d MyDatabase
It kind of sounds like you need something to just end the power-management? It would depend on how that is configured
I'm not familiar with Citrix, but it looks like there is a powershell module for monitoring/managing it and its database connections with cmdlets like Get-BrokerServerStatus for example:
https://support.citrix.com/article/CTX232979/how-to-connect-site-to-sql-ha-sql-mirror-or-a-new-sql-server-after-migrating-your-database

Using PsEXEC via SQL Server agent job

I have a need to add a step (a batch file that needs to run on the application server) once my backup from db server is complete. I am thinking of using the PsEXEC. For whatever reason the command I am testing via the query window executes the batch file at the app server just fine but when I add the same command as step in SQL server agent job it is giving me the error "The process could not be created for step 1 of job 0x318474904D93B54A81BA8B1AEE891A13 (reason: The system cannot find the file specified). The step failed."
Here is the command line running just fine from the query window
EXEC XP_CMDShell 'psexec -accepteula -u domain\user -p psswordhere \appserverip -s -d cmd.exe /c "F:\MovefFilesTest\TheJob\MoveTest.bat"'
FYI: domain\user is local admin on both servers and the sql service and agent are running under this account. It is also a sysadmin on the sql server itself.
Your help is appreciated.
Also F:\ is a local drive on the app server. I am puzzled why it is working on the query window but not when added as agent job

Running SQLCMD in a Batch Script for local .sql file on local SQLSERVEREXPRESS2014 throwing 'Access Denied' error

I have a created a .sql file that will update a table in a data warehouse I am creating using SQL Server 2014 Express. Since Express does not support scheduling jobs, I am attempting to use a batch script scheduled in task scheduler to accomplish this.
ECHO OFF
ECHO This is running to update the [Table Name] Table in the SQL Server Database
PAUSE
sqlcmd -S [ComputerName]\SQLEXPRESS2014 -U myusername -P mypassword -i "My Path With Spaces" of [MyQuery].sql
ECHO Query Worked!
PAUSE
However, when I run it (even as Administrator), it denies me access. I am the administrator on this computer and the SQL Server Express 2014 service is local to my machine. I appreciate any advise the community may be able to provide here. :)

Configure Jenkins to restore DB to a known dataset before running MsTest application

I have a MsTest project configured in Jenkins to test a ASP.Net MVC application.
And it's working fine.
What I need to do is, Need to restore a SQL Server database which is located on a different server that use by the MVC application, before I run the MsTest project on Jenkins.
So Can I do it as doing something like adding a step to restore the DB before the test project execution step?
And Is there any way to command (even using some plugins) remote SQL Server to restore a database through Jenkins?
REM restore DB (with REPLACE)
SQLCMD -U user -P password -S .\SQLEXPRESS -Q "RESTORE DATABASE databasename FROM DISK='path to back file'" WITH REPLACE
You need to put this script in a "batch windows cmd" in Jenkins (replace the user, password, databasename, and path to back file with the correct values).

SQL Server Agent run powershell file not found

I created a SQL Server Agent job and here is the job detail.
The job owner is my windows domain login.
The type is OS (cmdExec)
Run as SQL Server Agent Service Account
Command:
powershell.exe -executionpolicy bypass -file "D:\MyFolder\myFile.ps1".
When I ran the job it returned an error stating "D:\MyFolder\myFile.ps1" does not exist.
When I run the same command from cmd prompt it works find.
My question is what can I do to make SQL Server Agent Service Account "see" the file?
Thanks,
It's a privilege issue. I just granted read/execute permission to the Network Service login which SQL Server Agent uses.
It works now.
And thanks for all your responses.

Resources