SQL Server update query triggered by stored procedure - sql-server

Is there a way to have a SQL Server stored procedure trigger a query without editing the stored procedure code?
I am looking for a way to do something along the lines of a CREATE TRIGGER, but have it be triggered by a stored procedure instead of table update without altering the original code for the stored procedure.
I would create a trigger for when the table updates, but the stored procedure that updates the table updates it tens of thousands of times, so that is unfeasible.

select * from sys.dm_exec_procedure_stats
I would check the "execution_count" to the system table listed above. You can create a stored procedure and then create a job that runs that stored procedure at a specified interval.

Related

difference between calling and executing stored procedure in sql server

I have created a stored procedure using a table in the server. I had originally created the procedure and executed it a month back. The table that is used in the SP has been updated. So i want to call the SP again. Is the way to do that is to execute it again like i did a month ago, right cluck on the SP and click 'Execute Stored PRocedure...' or is there another way of calling it?

How to get name of executing stored procedure in Snowflake?

Does snowflake have function which returns name of the current stored procedure like the following in SQL Server.
SELECT OBJECT_NAME(##PROCID)
I am just trying to build a logging table to log all statements that are executed inside a stored procedure this is for monitoring purpose i.e. which statement within stored procedure failed and how long queries are taking to run. If Snowflake has something out-of-box OR a recommended way of doing it please share.
Try this from within your stored procedure:
const procName = Object.keys(this)[0];
Also see this related post.

SQL Server - trigger AFTER INSERT table in schema

I have a stored procedure to run after a set of tables, all belonging to the same schema [DATA_Countries], is dropped and then re-inserted.
The operation is performed by another application, which drops and recreates the target table, over which I have no control.
Since the table is dropped and recreated each time, I can not use triggers on each target table.
Is there a way to get a trigger for each time a table is inserted into a specific schema, to return the name of such table and launch a parametrized stored procedure?
Thanks!
Yes, You can create DDL Triggers on SQL Server to track the DDL Changes. For example, If I want to track the changes in Stored Procedures on my Database AdventureWorks, I can create a trigger like this
CREATE TRIGGER td_ProcTrack
ON AdventureWorks
FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE
AS
BEGIN
<my code>
END
Refer this Article for more detailed examples

Syntax for creating temp table with their unique name by select query in stored procedure using SQL Server

I want to create temp table with their unique name by a select query in a stored procedure in SQL Server.
For example: whenever I run the select query at that time different temp table name want to create.
Let be more clear, at the first time when I will run the select query at that time temptable name is temptable1, while at the second time the table name will be temptable2 and so on.
I want to know the syntax for executing the select query and creating the temptable with their unique name in a stored procedure in SQL Server.
In the context of the SQL Server Stored Procedure, the engine is handling itself the names of the temporary tables.
There is no need to worry if many users are executing the same stored procedure in same time - the temporary objects cannot be shared across them and no conflicts are going to happen.
Also, naming a temporary table in stored procedure with different name can be done using a dynamic T-SQL statement. You can for example, use a sequence to get such number and concatenate it to the table name. But, if you do so, you need to use sp_executesql to create your table and do things with it. In such way, no other stored procedure would be able to read/modify the table you have created in the current stored procedure. In other words, the temporary table cannot be shared over the routines if created using dynamic T-SQL statement. So, there is absolutely no point of doing such thing.

SQL Server stored procedure calling from another stored procedure

In SQL Server 2000, from one stored procedure (Master stored procedure) I am calling a series of other procedures.
The nth procedure is throwing a column not found error, but the nth procedure got executed just fine if I run it separately.
If I place this nth procedure at any place in the master stored procedure it behaves in the same way.
Can anyone please help me to find solution to this problem?
If the procedure is doing any type of DDL against a table, the same transaction cannot correctly reference the table. For example, if you have a stored procedure that runs a SELECT... INTO and then another statement that runs an ALTER TABLE... ADD columns, you will run into this issue.

Resources