Modify Stored Procedure Without Executing - sql-server

We have a stored procedure that fires off a thousand emails. It's auto-run at 1 AM every night. I have to edit the stored procedure in Management Studio: I know I have to choose "Modify" and then make the edits, but I don't want to click "Execute" to save it, or the emailing sequence will start. How can I save the new code without executing the stored procedure? Thank you

Clicking Execute doesn't execute the procedure itself, it executes only SQL command altering the procedure.
You can check it by creating some mock stored procedure that is meant to just add some data to table and check if it adds that data when you click Execute in Modify window.

Clicking Execute won't actually run the procedure, it just applies your modifications.
Comment out the part that triggers the emails to be sent then execute, if you would prefer to see what it does first. Then when you are comfortable, uncomment and make your changes.

Related

Display output messages after execution of each stored procedure?

Preferably without editing the stored procedure, is it possible that the output messages are displayed after every stored procedure completes execution, instead of at the very end?
For example, the following script (that calls an SP to insert 300K+ rows per day) will display the 4 output messages (300230 rows affected) once all SPs execute or if the process is cancelled.
exec usp_UpdateSales '11/21/2022'
exec usp_UpdateSales '11/22/2022'
exec usp_UpdateSales '11/23/2022'
exec usp_UpdateSales '11/24/2022'
Instead of showing the output messages at the end, is it possible that is displays the messages after the execution of each call?
I was reading about RAISERROR, but I'm not certain how to implement it into an SP call.
The SSIS Execute SQL Task does not hook the InfoMessage event. What you want from a problem statement, is to capture the messages printed to the screen in other applications.
As #AlwaysLearning points out, the approach is to create your own Script Task that runs your statement and then "does the thing" with the feedback from your stored procedure https://learn.microsoft.com/en-us/dotnet/api/microsoft.data.sqlclient.sqlconnection.infomessage
Some related sample bits of code to give you a solid shove in the right direction
c# - SqlConnection InfoMessage triggering only at end of execution
Accessing InfoMessages for SQL Server queries in C#
Ado.net Fill method not throwing error on running a Stored Procedure that does not exist

Will it help if I check if a SQL Server trigger is enabled before I try to disable it?

I'm trouble-shooting some performance problems in a SQL Server database, and one of the things I found is that the stored procedures frequently disable and re-enable triggers. I'd love to have the authority and the time to redesign the thing so that it doesn't do this, but I don't.
As things stand at the moment, it's entirely possible for one stored procedure to disable a trigger, and then for another stored procedure to execute and want to disable the same trigger. Am I right in thinking that when this happens, the second stored procedure will have to wait for the first stored procedure to re-enable the trigger and release it's Sch-M lock on the table, so that the second stored procedure can acquire it's own lock and disable the trigger that the first stored procedure just re-enabled? If so, would it help at all if I modified the stored procedures to check if the trigger is already disabled before attempting to disable it?
Am I right in thinking that when this happens, the second stored procedure will have to wait for the first stored procedure to re-enable the trigger and release
Yes, as long as the first procedure is holding a transaction open.
If so, would it help at all if I modified the stored procedures to check if the trigger is already disabled before attempting to disable it?
You can do this by checking is_disabled on sys.triggers, but when doing so you would have to read with (nolock) (read uncommitted isolation level).
Nick is quite right, you have wandered into a minefield here. Note that you won't be able to modify data in the table while the first procedure runs anyway

SQL Server : How to prevent Replication on Batch delete and allow on Daily basis delete operation?

I have a Transactional Replication running on SQL Server 2012. It replicates INSERT, UPDATE and DELETE.
Now im facing a condition (for DELETE Statement) where it forces us to :
Replicate only daily-basis DELETE Operation
DO NOT Replicate a Yearly-basis DELETE operation as we'd like to prune Data created < Current year
I suppose I can't use this (see image below) option for such conditon
What's a good way to achieve this ?
The downvote was not warranted. Here's your solution.
To NOT replicate the annual batch delete process, the assumption is that the delete process is being done inside some kind of stored procedure(s), correct? If so, what you do is enable "replicate execution of a stored procedure". Once you enable this, you then create a post snapshot script that creates dummy versions of these stored procedures. The stored proc is simply empty. This way when it's executed on the publisher, only the proc call is replicated and executed on subscriber. And since the proc is empty on the subscriber, nothing really happens.
If the yearly delete operation is NOT inside a stored proc, then you'll need to go back to development/operations team and tell them to put the archiving/maintenance call into a different stored proc so you can do the above.

is it possible to invoke SQL trigger programmatically?

I know under such circumstances, I have to use Stored Procedures,
but still I want to know if it is possible? If NO, Why? If YES, How?
First thing that comes to mind is
update yourtable
set yourcolumn = yourcolumn
-- consider a 'where' statement
I imagine this would invoke the trigger without changing anything. Thus being invoked with code.
You cannot call triggers directly. They are fired automatically when you perform an insert/update or delete on a table that has triggers. Therefore, you cannot call a trigger in a stored procedure.
Trigger needs to have deleted or inserted record when executes, and I cannot see how it can be passed...
By definition "a trigger" is a procedure that fires when a table is changed. I guess you could make it fire programmatically by doing update/delete/create on a table that has a trigger.
If you want a procedure that can be executed manually, then as you pointed out, you should just create a stored procedure.
If you want a procedure that can be executed as a trigger and manually, why not create a stored procedure and then create a trigger that simply has one line that fires that procedure?
If you are writing some test/diagnostics code and really need to invoke some trigger code, you might be able to use some meta API (I remember Oracle had something like that. Not sure about sql server, but it's got to have something) to extract the code out and massage it into a stored procedure. If you do this, as Alex_L already mentioned, you will have to somehow fake out the pseudo update rows which are typically accessible only to triggers.

SQL Server 2005 -Seeing SPs executing inside SPs

I am wondering if it is possible to see stored procs that are being executed by other stored procs in Sql Server Profiler, is it possible, and if so how?
If you profile using the SP::Starting event, you can see all the stored procedures that are executed. You want to make sure to get a couple of the profile columns included:
NestLevel - This shows the nesting level of SPs calling each other - so if Test calls EmbeddedTest then the SP::Starting event for Test wil show NestLevel=1 and the event for EmbeddedTest will show NestLevel=2
ObjectName - This shows the name of the stored procedure being executed
SPID - Session ID for the connection
So to find a calling sequence you need to look for an event with NestLevel greater then 1 and then look for the NestLevel=1 on the same SPID that preceeded the call. Of course this works for deeper nesting levels as well.

Resources