Snowflake stored procedures execution - snowflake-cloud-data-platform

I was trying to execute Snowflake stored procedures in Qlik Data Editor.
Snowflake stored procedures starts with CALL statement. But it will not return tabular result directly. I get a message, and we have select those results to get tabular result. I was not able to store the result message of CALL statement.
Please can anyone help how to execute a Snowflake stored procedure?
EX:
CALL "ODS_BI".Dimension('SK0009', 'DEBIT', 1, NULL);

You need to use RESULT_SCAN to retrieve the results from calling a stored procedure.
As example:
CALL <stored_proc_name>();
Retrieve result set:
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));
For more information read here.

if you want to return a usable dataset then you might need to look at UDFs rather than SPs.
If you have to use a SP then this documentation explains the options available to you: Stored Procedure Overview

Related

How to use print statement in sybase stored procedure when calling the stored procedure from application

I am using powerbuilder with Sybase ASE database. I am calling a stored procedure while doing an update from my application but I am getting an error in the stored procedure. I want to put some print statements in the stored procedure to check what the error is. How can I see the output of this print statement? How can I log the stored procedure?
Thanks in advance
Sharmistha
There is no log of these print statements and they are not considered part of the result set. They are in the same channel as the Sybase ASE error messages.
They should be returned to PowerBuilder, in the SQLCA, sqlerrortext is the field.
That's not how I used to debug stored procedures though. I found it easier to capture the parameters being passed to the stored procedure and use Sybase's ISQL command line tool, not the Java GUI, to see the error messages.
You can use function which will return the status code. Add "ON EXCEPTION RESUME" clause inside function and return global variable ##error to your application.
Use a custom error logging procedure to store data in a log table and read errors from it during or after execution.
Alternatively, you can try the PRINT statement inside the procedure to see if you can get the stdout output into PB.
Thank you all, I added some logs in powerbuilder instead to check for my problem.

How to pass dynamic parametrs to query in MS Power BI?

I need to build a report in Power BI. I can get the data for the report by calling the MS SQL Server stored procedure with parameters. Question: How can I pass dynamic parameters from a report to a stored procedure call? And the second question: one of the parameters is the user login. How to get it in Power BI and transfer the stored procedure call parameter to?
Go to query editor, and create the required parameters (from manage parameters)
Now create a new query that calls SQL procedure, you can pass hardcoded parameters for now.
Once data are imported, go to advance editor and replace the hard-coded parameter in the query part with the parameter you created, you will have to concat it.
refer: https://www.c-sharpcorner.com/article/execute-sql-server-stored-procedure-with-user-parameter-in-power-bi/

Sybase system procedure - how to get result?

Is there any way of saving the result set of a SYSTEM procedure in Sybase ASE 15?
For example, if i want to get details about all the columns of a table, i would use the following code: sp_columns 'TABLE_NAME'.
I want to save the result into a temporary table or get it by using a cursor to use it for other queries. Is there any way of doing it?
Note: I am aware i could write my query by using the system tables and get the same result, but if this is possible, i would prefer this method.
The system stored procedures are not intended to be used for inputs into other tables/procedures.
If you don't want to write your own queries, you can look at the code behind the stored procedure by using sp_helptext. For system stored procedures you need to be in the sybsystemprocs db.
use sybsystemprocs
go
sp_helptext sp_columns
go
From there you can take a look at what is being queried and just grab what you need.
It's also helpful to take a look at the Sybase ASE System Tables Diagram: This shows all the system tables, and all the relationships between tables.
You can also use proxy tables to store the output of the SP in table.
This example is very helpful

What is better to return a single value in TSQL stored procedure: a RETURN or an OUTPUT?

I have a small stored procedure that needs to return a count of records (a single value). What is better to use? A RETURN to return the result or an OUTPUT parameter to return the result?
I'm using C# / .Net to 'talk' to SQL server.
Use an output parameter.
It is less resource intensive to deal with a scalar parameter than a dataset via SELECT (or an OUTPUT clause etc) in the client
And RETURN is limited to int datatype. This doesn't matter here, but generally RETURN would be used for status and errors
If you already know that the execution of your SQL statement will return a single row of data, then you should always prefer stored procedures with output parameters in place of single-row select statements.
There are several advantages of using stored procedures over embedding simple SQL statements in your application code. On top of all those advantages, what you save is instead of opening a recordset for fetching the data, you simply call a stored procedure with output parameters. When you use a recordset, the query results returned by the data source object include data and metadata. Often the metadata is much larger than the data or is a significant part of the query results. Because of this, you may want to use a stored procedure with output parameters instead.
I think that return and output work in a similar way. But, in general, return is most commonly used to return a status result or error code from a procedure.
I will suggest to use OUTPUT parameter because it is less costly. Return can be used here as you are returning count (int type) but that is not advisable as return is used for status or error.

SQL DMO based code generator

I already have a code generator based on SQL DMO, that writes the a C# function for any stored procedure in by SQL Server 2008 database. Currenly however the code generator can only handle stored procedures that have input and output parameters. For stored procedures that return multiple records, the code generator returns a datatable with rows that each represent a output record.
Is there a way using SQL DMO to determine the fields that would be returned by a stored procedure if the output of a stored procedure is select * from Member where MemberID=1?
Thanks
My guess is that you cannot do this in DMO, since DMO relies on meta information stored in SQL Server, and the result set description of an SP is not stored in that way (as far as I know).
What you can do, however, is to have your generator execute the stored procedure inside a transaction, and analyze the resulting SqlDataReader. Have a look at the GetName(), GetFieldType() and GetSchemaTable() methods to construct your result class.
After execution, rollback the transaction (in case the SP makes any changes to the database).
You also might consider upgrading your generator to SMO, as MSDN states that DMO will not be supported in the future.

Resources