How to Run a Program from SQL

How to run a program from SQL?

I would be careful with xp_cmdshell because it can create quite a security hole. Check out this article from
http://www.databasejournal.com/features/mssql/article.php/3372131/Using-xpcmdshell.htm

If the service account has local administration rights then you can use this procedure to perform any windows operating system command.

Check out this similar question I asked some time ago. After some research I decided the security risk was too great for a production DB server. Your situation might be different and xp_cmdshell might be the answer.
SQL Server xp_cmdshell

How to run Application with SQL Database on a machine that dosen't have SQL Server Installed

You need to change the connection string to specify the computer the database is installed on. Something like the following:

connectionString="Data Source=machine\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KBank.mdf;Integrated Security=True;User Instance=True"

Please note: The database on the other machine needs to be configured properly so it can be accessed from other machines.

How can I run an exe file in SQL Server management studio

Generally it's insufficient File system permissions. Check and verify that user account that runs SQL Server (e.g. LocalSystem or NetworkService) has permission (Read+Execute) to the executable files.

Go to -> Services.msc and check your SQL server is running with current User / Administrator user authentication which has permission of accessing your file system.

xp_cmdshell requires permission to access "C:\WINDOWS\system32" check if it is accessible.

Also put exe file into inner folder of C: or D: i.e. "D:\dist\dbtest.exe"

How can I schedule a job to run a SQL query daily?

  1. Expand the SQL Server Agent node and right click the Jobs node in SQL Server Agent and select 'New Job'

  2. In the 'New Job' window enter the name of the job and a description on the 'General' tab.

  3. Select 'Steps' on the left hand side of the window and click 'New' at the bottom.

  4. In the 'Steps' window enter a step name and select the database you want the query to run against.

  5. Paste in the T-SQL command you want to run into the Command window and click 'OK'.

  6. Click on the 'Schedule' menu on the left of the New Job window and enter the schedule information (e.g. daily and a time).

  7. Click 'OK' - and that should be it.

(There are of course other options you can add - but I would say that is the bare minimum you need to get a job set up and scheduled)

Is there way to get SQL to launch a C# application

It it not favoured practice but you can use the below stored procedure to call a C# executable. Some times it is unavoidable if you are constrained.

xp_cmdshell { 'command_string' } [ , no_output ]  

You can find the documentation here

A way for a program to constantly run

There are many options:

If you have a dedicated server:

  • Create your application as a console application and put it in startup
  • Create your application as a console application and using Windows Task Scheduling run it with your required frequency.
  • Create it as a windows service that will always run

If you don't have a dedicated server:

  • Use libraries like Quartz to perform task scheduling in your web application


Related Topics



Leave a reply



Submit