SQL Server stored procedure or alternative method to restart SQL Server Agent - sql

There are various ways to restart the SQL Server Agent on a server, but I would like to do it from a stored procedure in one of my databases (on the same server). How would one go about doing that? Is there some sort of a system stored procedure that I could call? Or would I need to call some sort of third party library/external language to accomplish that such as the following?

Apparently one of my co-workers had already solved this:
EXEC xp_servicecontrol N'STOP',N'SQLServerAGENT';
-- Give the service a little time to stop
waitfor delay '00:00:10.000'
EXEC xp_servicecontrol N'START',N'SQLServerAGENT';
Not sure this the best solution, but should work for my purposes...

PowerShell could be a good approach depending on the circumstances. the Start-Service and Restart-Service commands would be a good starting point :O

Related

How to run stored procedure every second automatically (sql server 2014 Express)?

I want to run stored procedure every second automatically.As I am using SQL Server 2014 Express edition so it is not possible to do the same with SQL Server Agent. Also, I don't have any third party tools to do so. I know it can be achieved by using Task Scheduler, sqlcmd utility or by using third-party tools but I don't know the exact way to do that.
I know this can be done and therefore I'm sure it's something I've missed but if anyone can share their experience of this I'd very much appreciate it.
Thanks.
This is a tough one. You're on the right track that you need an outside method to run the job, but I'd recommend writing one yourself to do it if you need the frequency every second (and also question if you really need it every second or not). A service or light application would be the best way to handle it.
I've written an article on setting up Task Scheduler for automated jobs on SQL Server Express, but that has a limit of once/minute. You would use the sqlcmd utility in this case.
You can look at third party utilities like JAMS Scheduler, but their community edition is only free for a year.
That being said, I don't know of any quick-and-easy methods that satisfy your frequency requirements as stated.

SQL Server stored procedures automatically recompiled?

If I use a web application Web Data Administrator and I edit the stored procedures SQL query, does it recompile on it's own? (new to SQL Server and this side of the database development)
MSSQL Server does maintain a cache of query plans, but this is not the same as compiled code.
The SQL Server manages this cache and can be the source of some pain if it caches a plan that is non-optimal. Though this has happened to me less than 5 times in 15 years (and that seemed to be a problem with a particular server), its best to let SQL server handle this and not touch it.
You can force SQLServer to recompile by supplying the WITH RECOMPILE option. Same caveat applies, unless you have a substantial reason to, DONT.
SQL is a scripting language, which means the code you write is not compiled. Rather, it is stored on the server to be used later.
When you edit a stored procedure, you can execute an ALTER script, or a DROP then CREATE script. This sends the text in your Web Data Admin (or SSMS) window to the server, issuing a command that tells the server to store this new query as a procedure for later use.
So, in short, yes, if you execute an ALTER script.

Who is accesing my sql table?

I took over someones job and need to find out from where certain tables are being read. Is there a way to do that? The more information I could get about the caller the better.
I am using MS SQL Server 2000.
If you want to see the full detail of all queries that are being run against the server, including the hostname and username of the client calling it, you'll want to run a trace.
You can use the SQL Profiler tool to run a short trace, but I prefer to run a "server-side" trace to a file, which I can examine later.
See the SQLServerPedia article here: http://sqlserverpedia.com/wiki/The_Server-side_Trace:_What,_Why,_and_How
You might also run a network sniffer process to watch the raw connections.
You could add a trigger to the table you want to be audited and insert relevant data into an audit table from there.
E.g. http://www.devx.com/dbzone/Article/7939/1954
If you can get on the server while your table is being accessed, look into the undocumented stored procedure sp_who2.

Setting query timeout on a stored procedure in SQL Server 2005

Does anyone know how to set the timeout on a stored procedure? Found some examples on the NET, e.g sp_configure 'remote Query Timeout', 5, but this did not work. Also found some commands "DBPROP_COMMANDTIMEOUT" and "DBPROP_GENERALTIMEOUT" but i don't know if they are the right ones to use and if they are, how to use them in my transact-SQL code.
As Chris Tybur said, you can not the the query timeout for a stored proc in the stored proc or on the SQL Server.
CommandTimeout is a client concept: the client will abort the query after a certain amount of time. There is no dead man's timer or mechanism for a stored proc to abort itself /or any query). SQL server will allow a query to run forever.
The "Remote Query Timeout" is exactly that: timeout when SQL Server makes a remote call, when SQL Server itself is the client of another server. It says in the description:
This value applies to an outgoing
connection initiated by the Database
Engine as a remote query. This value
has no effect on queries received by
the Database Engine.
A recent question with good info: timeout setting for SQL Server
I have never heard of setting a timeout for executing a stored procedure on the server side. Usually you would specify the timeout for the command that runs the procedure in the data provider you are using, such as ADO.NET.
Wait - the real question is, "What is happening that you want to prevent?" Everyone has focused on server-side, client-side but in truth we don't know why you are asking this question (and it's important).
And another "why": why do you want to set a timeout on a "stored procedure"? Why not a view, a function, or a query? Did you use the term "stored procedure" for a particular reason or would you just be interested in learning how to set a timeout in T-SQL?
I'm asking because I wonder if you are having locking issues and perhaps SET LOCK_TIMEOUT 1000 or WITH(NOLOCK) might be what you really need. Without more info though I can't say. If you can give us more feedback on why you are asking, what is happening, and what ultimately you want to have happen if your "timeout" is reached, maybe we can help more.
Bottom line: Yes, you can set a timeout in T-SQL and yes you can stop the execution of a stored procedure with T-SQL. But I don't know enough about what you want to offer advice on where to look or to give you more info. I'm kinda scared that I've already said too much :)
This article has a very good explanation on query timeouts and how they're a client-side concept only. You can set the query timeout in SQL Server management studio from the Tools|Options menu.
You have to set the timeout when you execute the stored procedure on the client. As far as the SQL Server goes, it'll let the stored procedure run for ever unless told to cancel it.

How can I run sql server stored procedures in parallel?

I want to do something like:
exec sproc1 and sproc2 at the same time
when they are both finished exec sproc3
I can do this in dts.
Is there a way to do it in transact sql?
Or is there a way to do it with a batch script (eg vbs or powershell)?
You could create a CLR Stored Procedure that (using C#) would call the first two on their own threads, and then block until both are complete... then run the third one.
Are you able to use CLR sprocs in your situation? If so, I'll edit this answer to have more detail.
sp _ start _ job
I'm doing a similar thing at the moment, and the only way I've found to avoid using SSIS or some external shell is to split my load routine into 'threads' manually, and then fire a single master sqlagent job which in turn executes as many sp _ start _ job's as I have threads. From that point, they all run autonomously.
It's not exactly what we're looking for, but the result is the same. If you test the job status for the sub jobs, you can implement your conditional start of sproc 3 as well.
What's the point in 8 cores if we can't use them all at once?
Do you absolutely need both SPs to be executed in parallel?
With simple CRUD statements within a single SP, I've found SQL S. does a very good job of determining which of them can be run in parallel and do so. I've never seen SQL S. run 2 SPs in parallel if both are called sequentially from a T-SQL statement, don't even know if it's even possible.
Now then, do the DTS really execute them in parallel? It could be it simply executes them sequentially, then calls the 3rd SP after the last finishes successfully.
If it really runs them in parallel, probably you should stick with DTS, but then I'd like to know what it does if I have a DTS package call, say, 10 heavy duty SPs in parallel... I may have to do some testings to learn that myself :D
You can use SSIS. The benefits of this are that the package can be stored in the SQL Server and easily scheduled there.
From PowerShell or just about any external scripting language, you can use the SQL command line osql or sqlcmd. This technique can also be used to schedule it on the SQL Server by shelling out using xp_cmdshell also.