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
Related
I am planning to migrate a legacy application which was using SQL Server 2012 to latest SQL Server managed instance on Azure.
But I doubt it will work, because my database has lots of stored procedures which use SQLCMD and BCP tools.
For example:
EXEC master..xp_cmdshell #cmd;
FTP commands, PowerShell copy command which involves actual file storage.
It will execute fine on a standalone Windows machine where we have actual storage like C:/ or D:/
But how it will behave in latest SQL Server Managed Instance on Azure?
Please can you suggest any idea?
Will the same stored procedure code work fine in latest SQL Server Managed Instance on Azure?
xp_cmdshell is not supported on Azure SQL Managed Instance. Either migrate that stuff to a different solution, or use SQL Server on Azure VMs, which is super easy to set up and operate.
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
I am using SQL Server 2008 (Express Edition).
I want to create a job which will delete all data from the all the table(>50) in the DB everyday at night 1:00.
Instead of Deleteting i decide to restore the DB from the Script.
It would have been easy by using SQL Server Agent, But this is limitation in SQL Server Express.
I figured out that we can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler.
I have no clue what i have to write in bat file and sql file.This is my first time where i am working so deeply in SQL configuration. Can someone help please?
Name of the script which i need to restore is test.sql.
If any one has different approach , please share.
Thanks
Prat
Your batch files needs to look like this. Change the path to your .sql file and also put in the sql server info. You can read more about sqlcmd HERE. Also note the case on the switches -S and -i as it does matter.
sqlcmd -S <ComputerName>\<InstanceName> -i C:\test.sql
In SQL Server Management Studio I would like to know what is SQLCMD mode?
I did some more research, so here's my understanding of this to extend what has been written so far:
What is SQLCMD
SQLCMD.exe is a console utility included in the instalation of SQL Server 2005 and higher. You can typically find it in a path like c:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE.
It is a simple scripting environment that allows automation of tasks related to SQL server. For example, you can write and execute a script that will login to a specific instance of SQL Server, execute scripts from a given directory on this connection and store the output in a specified file.
Invoke-Sqlcmd cmdlet was introduced with SQL Server 2008 as a mean to replace this tool with a standardized, Powershell-based approach, preserving most of the original syntax and functionality.
What is SQLCMD mode in SSMS
In SSMS, SQLCMD mode is a script execution mode that simulates the sqlcmd.exe environment and therefore accepts some commands that are not part of T-SQL language.
Unlike sqlcmd.exe, it contacts the database using SqlClient (the same way as SSMS), not ODBC data provider, so in some aspects it might have different behaviour than sqlcmd.exe.
Executing scripts in SQLCMD mode allows to use commands typical to sqlcmd.exe environment. However, there's no IntelliSense or debugging support for SQLCMD mode, so maintaining scripts that mix clean T-SQL with SQLCMD-specific code can be a pain. Therefore, it should be used only when it's necessary.
Example use case
Let's suppose that a company has a naming convention for databases that include environment in the name, eg: MyDb_Prod, MyDb_Test, MyDb_Dev. This convention might be used to minimize chance of mistakes.
When a developer writes a T-SQL script, it will have to be executed in different environments in deployment/testing process, which would require many versions of the code:
SELECT *
FROM [MyDb_Dev].[dbo].[MyTable1] -- MyDb_Dev -> MyDb_Test -> MyDb_Prod
Instead, we can assume that database name will be provided as a SQLCMD variable in deployment process and have exactly the same file deployed to all environments:
-- :setvar databaseName "MyDb_Dev" -- uncomment for testing in SSMS
SELECT *
FROM [$(databaseName)].[dbo].[MyTable1]
(in this simple example database name could be omitted altogether, but if you have cross-database joins, using database name is necessary)
Exactly what it sounds like.
It is a mode that lets you author SQLCMD scripts.
From MSDN - Editing SQLCMD Scripts with Query Editor:
To use the Database Engine Query Editor to write or edit SQLCMD scripts, you must enable the SQLCMD scripting mode.
In SQL Server Management Studio, set this via the Query menu (Query -> SQLCMD Mode).
"You use SQLCMD scripts when you have to process Windows System commands and Transact-SQL statements in the same script."
"By default, SQLCMD mode is not enabled in the Query Editor. You can enable scripting mode by clicking the SQLCMD Mode icon in the toolbar or by selecting SQLCMD Mode from the Query menu."
Reference: MSDN
Message Transact-SQL IntelliSense is not active for this editor because the editor is in SQLCMD mode.
I was publishing SQL Server Project from Visual Studio to Database to synchronize changes and got an error above. Publishing was failing.
I closed all open files in VS and it resolved the issue.
I hope real life example explained why people use SQLCMD mode.
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