How to get name of executing stored procedure in Snowflake? - snowflake-cloud-data-platform

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.

Related

Data factory Copy Task - Using Stored Procedure containing temporary tables

In Data Factory we're using Copy Data task to move some data. The source is a SQL Stored Procedure and within the stored procedure some temp tables are being created. The Sink is an auto created physical SQL table (not a temp table).
When running the pipeline we get an error complaining about invalid object which is the temp table.
Failure happened on 'Source' side. ErrorCode=SqlOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=A database operation failed with the following error: 'Invalid object name `'#w_activity'.',Source=,''Type=System.Data.SqlClient.SqlException,Message=Invalid object name '#w_activity'.,Source=.Net SqlClient Data Provider,SqlErrorNumber=208,Class=16,ErrorCode=-2146232060,State=0,Errors=[{Class=16,Number=208,State=0,Message=Invalid object name '#w_activity'.,},],'`
The same sproc executes perfectly via SSMS. Having done some digging it looks like one solution is to re write the stored procedure to use table variables instead of temp tables.
Can anyone explain if there is a way that temp tables can be retained so that we don't have to re write all our stored procs, or if not can anyone explain why they can't/won't work. I understand about different sessions between the ADF activities, however these are being created/used within the same stored proc within a single activity.
Many Thanks
I tried to reproduce your scenario and got the similar error.
Using stored procedures, global temporary tables (##T), a workaround for doing this is to write stored procedure for getting data from temp table and execute it will give you a result.
Created stored procedure to fetch data from temp table.
create proc testtemp as
begin
select * from ##test
end
Using that stored procedures in copy activity.
Successful execution

SSRS Report Using Stored Procedure

I am working with an SSRS Report that uses a stored procedure.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with ALTER PROCEDURE ...
While I can understand the SQL in a stored procedure, I have never used one in an SSRS Report [I only use 'straight' SQL statements].
When I use SQL as my Dataset, I can copy that SQL into SSMS and run it and see the data it returns.
With this stored procedure, how do I execute it in SSMS to see the data it returns? The stored procedure has a sample 'EXEC ...' statement with all the parameters populated ... but when I run that - no data is returned.
The SSRS report runs fine, but I want to be able to work with the stored procedure in SSMS and see the data it is returning. My goal is to be able to run the stored procedure in SSMS and then tweak it.
How do I work with this stored procedure in SSMS so I can look at the output?
If you just want to execute the procedure in SSMS, locate it in the object browser ([DatabaseName]/Programmability/Stored Procedures). RIght-click the procedure and select 'Execute Stored Procedure'
Fill in the parameters and click OK and a script will be generated to run the procedure.
It's a bit overkill but at least everything is there and you can run it whenever you like.
If you want to edit the proc, right-click and choose modify, a new script will be created (the ALTER PROCEDURE script you mentioned). Make changes as required, run the script and that will modify the procedure, then execute the procedure to see the results.
Of course it would be safer to make a copy and edit that, you can also just run the body of the stored proc by commenting out the ALTER PROCEDURE statement until you are happy with it but you may have to declare and variables that are normally passed in as parameters.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with
ALTER PROCEDURE ...
That's the Alter Procedure script. Use this to edit a stored procedure.
In other words, edit the SQL code you want to optimize, then run the whole script to save the changes.
How do I work with this stored procedure in SSMS so I can look at the
output?
In SSMS use the syntax for stored procedures:
EXEC myspname paramter1, parameter2, param...
Where parameter1, parameter2, etc. are the parameters described in the ALTER Procedure script, directly after the ALTER PROCEDURE myspname. Parameters are preceded by the # symbol.
As you type-in the EXEC procedure command pop-up hints should appear describing the parameter.
Without knowing the code to the stored procedure, it could be doing any number of things based on what is passed to it by parameter. A stored procedure can do DDL and DML queries, and does not necessarily have to select anything at all for output.

SQL Server update query triggered by stored procedure

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.

How to execute dynamic stored procedure in sql server with boolean return value?

I have a stored procedure that has boolean result parameter as output. But my project needs to use 3 databases. Basically, there is a main database and 2 others. The other databases using same stored procedure but they just depend on params. If I explain the scenario, you will understand. Firstly, Sorry for bad explaining.
This application using main database for session management, user configurations and so on. This is okay. The problem is same user has to use 2 different databases for creating invoices. We can pretend that these users are IT support staff. They works for 2 different companies and supporting their products and they are managing their solutions in different databases.
User
Main Database
A Company Data
B Company Data
Users have to create their invoices for each company's customers. Basically they are using same database but the name of databases are totally different. Maybe my problem has easier solution so I want to ask that How can I use Dynamic Database Name in my Stored Procedure? and my current question is I'm initialising Stored Procedure as String and after that I execute the stored procedure as String.
Procedure has no error, also says me completed successfully but there is no insert (in stored procedure). When I use the sql command (which I set manually as String) in Management Studio, Query is running perfectly.
As a summary,
I have 3 databases, Main database need to execute generating Invoice stored procedure. But database names are must be dynamic because of different companies.
When I send the database name A_COMPANY_DB, stored procedure should execute in A_COMPANY_DB. When I send B_COMPANY_DB, stored procedure should execute B_COMPANY_DB.
Both of A_COMPANY and B_COMPANY databases are same. I have to manage sql query as String because of Dynamic Database Name. So I can't reach the output parameter.
My stored procedure has only one output parameter which is bit type. But I can't use it like:
Set #dynsql = 'USE ' + QUOTENAME(#DbName) + ' exec.[dbo].[spName] ' + other params
EXECUTE sp_executesql #dynsql
In this situation I couldn't reach Output parameter. How can I set or use my Output parameter in this stored procedure?
EXECUTE sp_executesql #dynsql #outputparam OUT
Is it possible or any solution?
Sorry for bad explaining.

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