How to SET NOCOUNT ON/OFF inside a Netezza stored procedure? - netezza

Based on NZ documentation, it seems NZSQL equivalent command NO_ROWCOUNT will suppress row count information. But how do I execute/apply it inside a NZ stored procedure?
I've tried to create and executed a NZ SP that contains the command "SET NO_ROWCOUNT" with error "expecting an identifier found a keyword".
My sample code as follows:
CREATE OR REPLACE PROCEDURE MYSCHEMA.MYSP()
RETURNS CHARACTER VARYING(ANY) EXECUTE AS CALLER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
iTot INTEGER;
BEGIN
--SET NO_ROWCOUNT;
SELECT COUNT(1) INTO iTot FROM MYSCHEMA.MYTABLE;
RETURN iTot;
END;
END_PROC;
Pls advise.
TQ in advance!

Related

Snowflake sql Procedure

Am trying to create a procedure using sql in snowflake,but its giving an error when calling it.
create or replace procedure get_max_date( )
returns datetime not null
language sql
as
$$
begin
set max_date= (select max(last_updated) from Control_Variables);
return max_date;
end
$$;
Error:SQL compilation error: error line 5 at position 9 invalid
identifier 'MAX_DATE'
please give me a solution ,actually i want to declare a variable inside proc and store data in that variable as shown in procedure
Regards,
Nadeem
Tried to create snowflake procedure
Indeed, you need to declare your variable inside the procedure. Try this:
create or replace procedure get_max_date( )
returns datetime not null
language sql
as
$$
declare
max_date datetime;
begin
max_date := (select max(last_updated) from Control_Variables);
return max_date;
end
$$;
Example how it works with some test data:
create or replace table Control_Variables (last_updated datetime);
insert into CONTROL_VARIABLES values ('2022-11-28 23:59:59');
insert into CONTROL_VARIABLES values (current_timestamp());
call get_max_date();

write multipe statments in snowflake

Hi i have one doubt in snowflake how to write multiple update stments using stored procedure.
i have tried like below
create or replace procedure sp_multipleupdate()
returns table()
lANGUAGE sql
as
$$
declare res rsultset(
update TEST.PUBLIC.DEPT set Dname='PM' where deptid=10;
update TEST.PUBLIC.emp set name='veavi' where deptno=20;
update TEST.PUBLIC.loc set locname='del' where id=5;
)
begin
return table(res);
end;
$$;
getting error :
000006 (0A000): Multiple SQL statements in a single API call are not supported; use one API call per statement instead.
Syntax error: unexpected '('. (line 2)
please let me know how to write query to achive this task in snowflake server .
Multiple SQL statements inside the resultset are not supported.
Rather than writing the UPDATE statements like that I would create a more generic procedure and pass arguments to it, so maybe split the above one in 3 procedures since these UPDATE statements are for different tables.
Here is a sample of a generic stored procedure:
create or replace procedure find_invoice_by_id_via_execute_immediate(id varchar)
returns table (id integer, price number(12,2))
language sql
as
declare
select_statement varchar;
res resultset;
begin
select_statement := 'SELECT * FROM invoices WHERE id = ' || id;
res := (execute immediate :select_statement);
return table(res);
end;
You can read more here.

How do i execute a stored procedure with a parameterised output from within another stored procedure

Consider the following stored procedure:
ALTER PROCEDURE Administration.SetAndRetrieveNewPurchaseOrderNumber
#PurchaseOrderNumber INT OUTPUT
AS
BEGIN
SET NOCOUNT ON
UPDATE Administration.KeyNumbers
SET PurchaseOrderNumber += 1
WHERE RowId = 1
SET #PurchaseOrderNumber = (SELECT kn.PurchaseOrderNumber
FROM Administration.KeyNumbers kn
WHERE kn.RowId = 1)
END
GO
I can use this easily from within my application by simply executing the procedure and passing in by reference a suitably named parameter.
I now find myself wanting to execute the procedure listed above in another stored procedure. I tried the following, but it doesn't appear to work (either with or without the # symbol in the parameter part of the stored procedure being called;
DECLARE #PurchaseOrderNumber INT
EXEC Administration.SetAndRetrieveNewPurchaseOrderNumber(#PurchaseOrderNumber)
What is the correct way to do this, or in reality should there be a separate procedure for use in circumstances like this that only produces a scalar result?
You need to add the output keyword when passing in the parameter.
For example:
Declare #output int;
Exec storedproc(#parameter output)

Error when trying to execute stored procedure

When trying to run a stored procedure, I get the following error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '#CSVPath'.
I'm trying to execute it using the following:
EXEC dbo.ProcessData #CSVPath = 'D:\Data.csv'
My stored procedure starts like this:
ALTER PROCEDURE [dbo].[ProcessDataData]
#CSVPath varchar(MAX) --Path to CSV containing data.
AS
BEGIN
{query}
END
I don't know what I'm doing wrong when passing the parameter value.
Thank you.
Assuming Your proc name has the word data only once, I have just managed this successfully:
create PROCEDURE [dbo].[ProcessData]
#CSVPath varchar(MAX) --Path to CSV containing data. AS BEGIN
select #CSVPath END
EXEC dbo.ProcessData #CSVPath = 'D:\Data.csv'
Output:
D:\Data.csv
First confirm with stored procedure name and use the same SP name so that u will not get this kind of errors
You are executing wrong sp
use below query it will execute
EXEC dbo.ProcessDataData #CSVPath = 'D:\Data.csv'
This is work...your Procedure name is [dbo].[ProcessDataData] and you execute EXEC dbo.ProcessData....please like that execute EXEC dbo.ProcessDataData its work for mee...
ALTER PROCEDURE [dbo].[ProcessDataData]
#CSVPath varchar(MAX) --Path to CSV containing data.
AS
BEGIN
-- Query to execute data
END
EXEC dbo.ProcessDataData #CSVPath = 'D:\Data.csv'
Hope Its Work !!!
Happy Coding !!!
Try it, may be it works for you;
EXEC dbo.ProcessData 'D:\Data.csv'
It'll auto map your data to the parameter: #CSVPath. Otherwise, may be the issue was in your query. If issue still exists, share your complete StoredProcedure for better judgment and solution.

Total number of Result Sets in Stored Procedure

In SQL Server 2008, is there any way to get the total numbers of Result sets (tables) been populated after an execution of stored procedure.
Lets say I have one stored procedures which internally calls another stored procedure. I want to know that how many result sets it returns that internally called stored procedure.
Can anybody can assist me on this.
e.g.
CREATE PROCEDURE sp_GetReports
(
#reportName AS VARCHAR(50)
)
AS
BEGIN
DECLARE #reportProcName AS VARCHAR(50)
SELECT #reportProcName = ReportProcName
FROM ReportMaster
WHERE ReportName = #reportName;
EXEC (#reportProcName)
/*
* Need to get here, total numbers of Result Sets (tables) retrived.
*/
END
GO
Thanks in advance.
You might be able to:
Get the row count in the stored procedure and retrieve the result as an OUTPUT parameter
Utilize some type of external table (would have to design this)
This gives you the number of rows returned from the stored procedure
USE Northwind
GO
CREATE OR ALTER PROCEDURE dbo.Test (
#rowCnt int OUTPUT
)
AS
SELECT * FROM dbo.Customers
SET #rowCnt = ##ROWCOUNT
GO
DECLARE
#rowCount int
EXEC dbo.Test #rowCount OUTPUT
SELECT #rowCount
What about a workaround?
Create some counter variable in each report.
Every time before SELECT statement, increment it.
Then, return it as the last output in sproc.
Each report should follow this convention.
You can return an output parameter with ##ROWCOUNT from inner procedure and use it in sp_GetReports.

Resources