How to execute a .sql Script in Kubernetes? - sql-server

I wrote a SQL script for SQL Server where I create and fill some tables.
Now this script needs to be executed every weekend.
We have a Kubernetes cluster.
So I am thinking of using a cron job (kind: CronJob)
That cron job should somehow trigger the SQL script.
Do I need to put the SQL script in the SQL Server?
How can I trigger it from the Kubernetes pod? Ideally I connect to the database with username and password and that's it.
That's just the way I would do it.

Related

How do I get SQL Server agent to show up in SSMS [duplicate]

Could anyone please explain to me how to create jobs in SQL Server Express edition?
SQL Server Express doesn't include SQL Server Agent, so it's not possible to just create SQL Agent jobs.
What you can do is:
You can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler.
For example, you can backup your database with two files like this:
backup.bat:
sqlcmd -i backup.sql
backup.sql:
backup database TeamCity to disk = 'c:\backups\MyBackup.bak'
Just put both files into the same folder and exeute the batch file via Windows Task Scheduler.
The first file is just a Windows batch file which calls the sqlcmd utility and passes a SQL script file.
The SQL script file contains T-SQL. In my example, it's just one line to backup a database, but you can put any T-SQL inside. For example, you could do some UPDATE queries instead.
If the jobs you want to create are for backups, index maintenance or integrity checks, you could also use the excellent Maintenance Solution by Ola Hallengren.
It consists of a bunch of stored procedures (and SQL Agent jobs for non-Express editions of SQL Server), and in the FAQ there’s a section about how to run the jobs on SQL Server Express:
How do I get started with the SQL Server Maintenance Solution on SQL Server Express?
SQL Server Express has no SQL Server Agent. Therefore, the execution of the stored procedures must be scheduled by using cmd files and Windows Scheduled Tasks. Follow these steps.
SQL Server Express has no SQL Server Agent. Therefore, the execution
of the stored procedures must be scheduled by using cmd files and
Windows Scheduled Tasks. Follow these steps.
Download MaintenanceSolution.sql.
Execute MaintenanceSolution.sql. This script creates the stored procedures that you need.
Create cmd files to execute the stored procedures; for example:
sqlcmd -E -S .\SQLEXPRESS -d master -Q "EXECUTE dbo.DatabaseBackup #Databases = 'USER_DATABASES', #Directory =
N'C:\Backup', #BackupType = 'FULL'" -b -o C:\Log\DatabaseBackup.txt
In Windows Scheduled Tasks, create tasks to call the cmd files.
Schedule the tasks.
Start the tasks and verify that they are completing successfully.
The functionality of creating SQL Agent Jobs is not available in SQL Server Express Edition. An alternative is to execute a batch file that executes a SQL script using Windows Task Scheduler.
In order to do this first create a batch file named sqljob.bat
sqlcmd -S servername -U username -P password -i <path of sqljob.sql>
Replace the servername, username, password and path with yours.
Then create the SQL Script file named sqljob.sql
USE [databasename]
--T-SQL commands go here
GO
Replace the [databasename] with your database name. The USE and GO is necessary when you write the SQL script.
sqlcmd is a command-line utility to execute SQL scripts. After creating these two files execute the batch file using Windows Task Scheduler.
NB: An almost same answer was posted for this question before. But I felt it was incomplete as it didn't specify about login information using sqlcmd.
SQL Server Express editions are limited in some ways - one way is that they don't have the SQL Agent that allows you to schedule jobs.
There are a few third-party extensions that provide that capability - check out e.g.:
Express Agent for SQL Server Express: Jobs, Jobs, Jobs and Mail (latest update is from 2005, it isn't maintained anymore).
SQL Scheduler

Built-in scheduler functionality in SQL Server 2008

My MVC3 application is using an SQL Server 2008 to store data. In particular - support ticket management data.
I have a table in a database - Tickets.
I are reviewing a possibility of implementing a recurring ticket registration, using an SQL Server features.
Is there a built-in SQL Server functionality, that would allow me to schedule, a, for example, once a week creation of a row in a database table?
I would use the SQL Server Agent and run a Transact-SQL Job Step.
Note that the agent runs as a service under a particular account, which will need rights to be able to carry out whatever operations your need.
Unfortunately you haven't mentioned your SQL Server Edition. If you have Express then there is no built-in scheduler so you need to use the Windows scheduler to run a batch file or other program that connects to SQL Server.
If you have any other edition, then you have SQL Agent which is a full scheduler with support for just about any task including running SQL statements.
You can easily use the Windows SchedTask Control Panel to schedule a batch file to run periodically:
In the batch file create a SQL string like so:
SET SQLSTRING=INSERT INTO Persons^
VALUES (4,'Nilsen', 'Johan', 'Bakken 2',^
'Stavanger');
Then, just enable delayed expansion and use something like this:
sqlcmd.exe -b -S localhost -E -d !DBNAME! -Q "!SQLSTRING!" -W

I need to automate SQL Azure database backup in SQL Script files. How can i do so?

Presently i am generating SQL scripts everyday to take the backup of SQL Azure database. I need to automate this process using some freeware. How can i achieve this?
Note:- I need to take the backup in form of SQL Script to some local machine.
Option 1: Run some code/script to backup the database
You could run the SQL using the SQLCMD utility.
http://msdn.microsoft.com/en-us/library/ms162773.aspx
To schedule this, just use the Windows Task Scheduler.
http://msdn.microsoft.com/en-us/library/bb756979.aspx
Option 2: Use Azure Data Sync
Keep a local database in sync with your SQL Azure database, like a constant backup.
http://msdn.microsoft.com/en-us/library/windowsazure/hh456371.aspx
Option 3: Use BacPac
There is an API available in Azure to backup your database to a BACPAC format.
http://msdn.microsoft.com/en-us/library/windowsazure/hh335292.aspx
A RedGate tool then allows you to restore to a local SQL Server.
http://www.red-gate.com/products/dba/sql-azure-backup/
Edit
I have since created a command line tool which will create a copy of your database, and back it up to BacPac using the Azure Management API:
https://github.com/richorama/SQLDatabaseBackup
You could try RedGate Azure Sql backup http://www.red-gate.com/products/dba/sql-azure-backup/
That can be also scheduled using Task Scheduler.
I have written some ideas on how to do that:
http://anttitech.wordpress.com/2011/11/06/azure-sql-backup-and-restore-scenarios-using-bacpac-exportimport/
SQL Azure Database does not support SQL Server Agent or jobs. You can,
however, run SQL Server Agent on your on-premise SQL Server and
connect to SQL Azure Database.
Source: http://msdn.microsoft.com/en-us/library/windowsazure/ee336245.aspx
SQL Server Agent is a Microsoft Windows service that executes
scheduled administrative tasks, which are called jobs...For example,
if you want to back up all the company servers every weekday after
hours, you can automate this task. Schedule the backup to run after
22:00 Monday through Friday
Source: http://msdn.microsoft.com/en-us/library/ms189237.aspx
Create an in-the-cloud backup of the database (to get a consistent snapshot) and then use the Data-Tier Application Framework (DACFx) client tool to copy the snapshot to your local machine, as described in Automating Windows Azure SQL Database Backup.
I have come across a free tool that makes backups of your SQL Azure Databases. Backups can be stored locally or on blob. You can also schedule backup operations with its built-in scheduler, or use the Windows Scheduler and the provided command-line utilities.
Source: http://blog.idera.com/sql-server/backup-and-recovery/announcing-new-free-tool-azure-sql-database-backup/

How to create jobs in SQL Server Express edition

Could anyone please explain to me how to create jobs in SQL Server Express edition?
SQL Server Express doesn't include SQL Server Agent, so it's not possible to just create SQL Agent jobs.
What you can do is:
You can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler.
For example, you can backup your database with two files like this:
backup.bat:
sqlcmd -i backup.sql
backup.sql:
backup database TeamCity to disk = 'c:\backups\MyBackup.bak'
Just put both files into the same folder and exeute the batch file via Windows Task Scheduler.
The first file is just a Windows batch file which calls the sqlcmd utility and passes a SQL script file.
The SQL script file contains T-SQL. In my example, it's just one line to backup a database, but you can put any T-SQL inside. For example, you could do some UPDATE queries instead.
If the jobs you want to create are for backups, index maintenance or integrity checks, you could also use the excellent Maintenance Solution by Ola Hallengren.
It consists of a bunch of stored procedures (and SQL Agent jobs for non-Express editions of SQL Server), and in the FAQ there’s a section about how to run the jobs on SQL Server Express:
How do I get started with the SQL Server Maintenance Solution on SQL Server Express?
SQL Server Express has no SQL Server Agent. Therefore, the execution of the stored procedures must be scheduled by using cmd files and Windows Scheduled Tasks. Follow these steps.
SQL Server Express has no SQL Server Agent. Therefore, the execution
of the stored procedures must be scheduled by using cmd files and
Windows Scheduled Tasks. Follow these steps.
Download MaintenanceSolution.sql.
Execute MaintenanceSolution.sql. This script creates the stored procedures that you need.
Create cmd files to execute the stored procedures; for example:
sqlcmd -E -S .\SQLEXPRESS -d master -Q "EXECUTE dbo.DatabaseBackup #Databases = 'USER_DATABASES', #Directory =
N'C:\Backup', #BackupType = 'FULL'" -b -o C:\Log\DatabaseBackup.txt
In Windows Scheduled Tasks, create tasks to call the cmd files.
Schedule the tasks.
Start the tasks and verify that they are completing successfully.
The functionality of creating SQL Agent Jobs is not available in SQL Server Express Edition. An alternative is to execute a batch file that executes a SQL script using Windows Task Scheduler.
In order to do this first create a batch file named sqljob.bat
sqlcmd -S servername -U username -P password -i <path of sqljob.sql>
Replace the servername, username, password and path with yours.
Then create the SQL Script file named sqljob.sql
USE [databasename]
--T-SQL commands go here
GO
Replace the [databasename] with your database name. The USE and GO is necessary when you write the SQL script.
sqlcmd is a command-line utility to execute SQL scripts. After creating these two files execute the batch file using Windows Task Scheduler.
NB: An almost same answer was posted for this question before. But I felt it was incomplete as it didn't specify about login information using sqlcmd.
SQL Server Express editions are limited in some ways - one way is that they don't have the SQL Agent that allows you to schedule jobs.
There are a few third-party extensions that provide that capability - check out e.g.:
Express Agent for SQL Server Express: Jobs, Jobs, Jobs and Mail (latest update is from 2005, it isn't maintained anymore).
SQL Scheduler

Database task to delete table records periodcally in sql server 2008

I want to add a database task that runs on a 6 hours interval. The task is to delete some records that matches certain condition.
What is the best way to achieve this in Sql Server 2008 ?
I know I can do this on the app side but I want it on sql server side.
You can use Sql server agent to run a job periodically. The job can do anything you want such as executing stored procedure which will do the actual cleaning.
Set up a task as maintenance task / scheduled task in the SQL Server admin.
There are several options. Two I would suggest:
Set up SQL Agent Job, with a T-SQL step in it. In the step details you will select target database and type in SQL statement to delete rows.
Create SSIS package; use Execute SQL Task and define SQL statement there. Then create SQl Agent Job to call the SSIS package.
(1) has a limitation - it will only be able to run the script against a database locate in the same instance of the SQl Server as SQl Agent, unless you use Linked servers.

Resources