Multiple Session executing Snowflake Procedure Insert into same Table - stored-procedures

We have a snowflake procedure that insert record into a table, something similar to below procedure. When this procedure is called by multiple application in multiple session, it is behaving erratic and throwing error. I understand when multiple session is trying to write to same table it is throwing error. Is there any work around to handle this scenario, since we do want to duplicate this proc for every application. Thanks in advance.
CREATE OR REPLACE PROCEDURE "SP_CRT_METADATA"(SOURCE_TYPE VARCHAR, BATCH_ID FLOAT)
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
EXECUTE AS OWNER
AS '
hist_col_load =` INSERT INTO SHRD_COL_MSTR_HIST SELECT * FROM SHRD_COL_MSTR WHERE SRC_TYPE =''`+SOURCE_TYPE+`'' AND ACTIVE_IND =''Y''`;
snowflake.execute({sqlText: hist_col_load});
'

Related

Stored procedure to insert new records in the child tables

I'm trying to create a stored procedure to insert new records in a number of child tables (using Greenplum) I have a master table and a set of child tables. I would like to insert new records that I have in the master table into the child tables (I have around 20 child tables). My assumption is that I should create a function, then a trigger.
Note that I only want to insert some fields into the child tables.
I made a few attempts, but here's the last one: (sorry in advance if it looks very bad. I never created any trigger functions)
FUNCTION:
CREATE OR REPLACE FUNCTION
schema1.newcustomerdata() RETURNS trigger AS $new_customer_data$
BEGIN
INSERT INTO schema1.customeridentifiers
(customer_id,
date_time)
SELECT NEW.customer_id,
date_time
FROM schema1.customersmaster
;
RETURN NEW;
END;
$new_customer_data$ LANGUAGE plpgsql;
TRIGGER:
CREATE TRIGGER newcustomerdata
AFTER INSERT ON schema1.customersmaster
FOR EACH ROW EXECUTE PROCEDURE newcustomerdata();
The function and trigger runs. However, I can't insert data in the master table anymore.
I get this error message:
function cannot execute on segment because it issues a non-select statement
So my questions are:
What would be the best solutions to update the child tables?
What's wrong with my function or trigger?
What are your recommendations?
Apparently "Triggers are not supported since they typically rely on the use of VOLATILE functions" Use RULES instead of triggers
CREATE RULE newcustomerdata AS ON INSERT TO schema1.customersmaster
DO also INSERT INTO schema1.customeridentifiers VALUES (NEW.customer_id, NEW.date_time);

PL SQL - Procedure - table that holds a record of users who executed a procedure

Hi and apologies in advance if the question has already been asked. I haven't been able to come across the answer.
I'm wondering if there is a table that holds a record of oracle usernames that have executed a particular procedure or function.
I'm trying to create a procedure that can be called as a subprogram by another procedure. The procedure which i'm looking to create will create a log entry every time the other procedure is executed. Example below;
User_Name = The Oracle user name of the person who executes the function.
Name = The name of the procedure or function.
LastCompileDT = The date/time the function or procedure was last compiled.
I'm a bit stuck on where to source the data from.
I've come across the all_source table but it only gives me the owner of the procedure and not the executing user.
Any feedback would be greatly appreciated.
Thanks
There might be a couple of ways to do that. Maybe someone else can suggest a method of extracting all this data from one data dictionary view. However, my method would be like this:
User_Name: use the keyword USER. It returns the Oracle user that executed the procedure:
SELECT USER FROM DUAL;
However, if you are interested in the OS user who executed that procedure, then you can use the following
SELECT sys_context( 'userenv', 'os_user' ) FROM DUAL;
More on this here. To my knowledge, this can be fetched on the fly only, and it is not logged anywhere by default. So you need to run it when you call the procedure.
Procedure Name: &
LastCompileDT : can be fetched from the view USER_OBJECTS
SELECT OBJECT_NAME, LAST_DDL_TIME
FROM USER_OBJECTS
WHERE OBJECT_TYPE = 'PROCEDURE'
AND OBJECT_NAME = '<YOUR PROCEDURE NAME>';
Rather than rolling your own audit, you could use the inbuilt auditing table provided.
See https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4007.htm
--Create a test procedure as an example
CREATE PROCEDURE my_test_proc
AS
BEGIN
NULL;
END my_test_proc;
--Turning on auditing executions of the proc
AUDIT EXECUTE ON my_test_proc BY ACCESS WHENEVER SUCCESSFUL;
--Run the proc
EXEC my_test_proc;
--check audit history
SELECT *
FROM dba_common_audit_trail cat
WHERE cat.object_name = 'MY_TEST_PROC';
The dba_common_audit_trail table has columns DB_USER, and OBJECT_NAME for your User_Name/Name.
For the last compiled time see Hawk's answer, or if you want to see a history of last DDL times you can add this to the audit
--Turn on auditing of creating procs
AUDIT CREATE PROCEDURE BY ACCESS;

DB2 calling stored Procedure in conditional statement

I need to query a stored procedure and on the basis of result set of that procedure need to make decisions in conditional statement
For instance I have a stored Procedure "Main_SP"
Now if result of "Main_SP" is 'null' then the result should be 'Tweety', but if th result set is not null then result set should be retrieved,
how to do it?
I tried following and some other but none worked.
SELECT
case Main_SP('MyVariable')
when 'null'
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
SELECT
case Main_SP('MyVariable')
when null
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
It is failing the condition, In first command when even it is 'null' it is not printing 'Tweety'.
and while using second, Getting error that 'Null' is not valid in the context.
I don't believe you can use a stored procedure that way.
A stored procedure can return data in two ways
As a result set
in an output or input/output parm
You're not using option 2 and I don't think a result set is ever NULL. There might be no records, but the RS itself isn't NULL. In addition, from what I can see in the manuals, processing a RS from a stored proc inside another stored proc in DB2 requires you to declare an allocate result set locators. But I've never done this.
If your procedure returns a single value or null, you'd be better served by defining it as a function with a return value. Then your code would be simply:
SELECT
COALESCE(Main_Fnc('MyVariable'),'Tweety')
FROM
SYSIBM.SYSDUMMY1 WITH UR
You can't call a stored procedure as part of an SQL fullselect; you would need to have the client that calls the stored procedure handle this logic. If you must do it on the server, implement a new stored procedure that calls Main_SP itself and contains your logic to interpret the result.

stored procedure which return fields unless it's used by code via datatableadapter

I have got an issue to retreive stored procedure result ( Table ) into a data table,
when i try to run my procedure from sql server , it's working, same as testing it from a dataset ( I have used a datatableadapter which invoque my stored procedure.
but when i try to get my stored procedure via code, it return an empty datatable.
here is what my query look like (it contain a lot of field so i tried to resume all,
CREATE TABLE #TemporaryTable(Code_Suivi smallint IDENTITY(1,1), DATECREATION date, NOMPROJET varchar(20), ProducedHours decimal(10,1), Accumulation)
insert into #TemporaryTable ( DATECREATION , NOMPROJET , ProducedHours )
SELECT query -- it will insert into #TemporaryTable what I have selected
to avoid getting an empty I have to not work with insert into... which is impossible for me, have any one encountered this befoure??
I have used another way to get it work, by avoiding datatable adapter, and using sqldatasource to invoque a select query

Informix 4GL and triggers

I want a simple SQL (trigger) for duplicate checking.
My table name is test1 with 2 columns, code and sname. Before inserting a new record, check if the record already exists: if it does, generate an error and do not insert; if it does not, let the insert proceed.
How do I do that?
The simplest, most reliable way to ensure that there is no duplicate data in the table is not using triggers at all, but using UNIQUE or PRIMARY KEY constraints:
CREATE TABLE test1
(
code INTEGER NOT NULL PRIMARY KEY,
sname VARCHAR(32) NOT NULL UNIQUE
);
The four constraints (two NOT NULL, one PRIMARY KEY, one UNIQUE) automatically ensure that no duplicate records are inserted into the table.
If you choose to add a trigger, it will be duplicating the work that is done by these constraints.
As to how to do it, you will need to create a stored procedure which is invoked from the trigger statement. It will be given the new code and new name, and will do a SELECT to see whether any matching record occurs, and will raise an exception if it does and will not raise an exception if not.
CREATE PROCEDURE trig_insert_test1(ncode INTEGER, nname VARCHAR(32))
DEFINE ocode INTEGER;
FOREACH SELECT code INTO ocode
FROM test1
WHERE code = ncode OR sname = nname
RAISE EXCEPTION -271, -100, "Value to be inserted already exists in table test1";
END FOREACH;
END PROCEDURE
Then you use:
CREATE TRIGGER ins_test1 INSERT ON test1
REFERENCING NEW AS NEW
FOR EACH ROW (EXECUTE PROCEDURE ins_trig_test1(new.code, new.sname))
In Informix 4GL, you can either create strings containing these statements, and then PREPARE and EXECUTE (and FREE) them, or you can use SQL blocks:
SQL
CREATE TRIGGER ins_test1 INSERT ON test1
REFERENCING NEW AS NEW
FOR EACH ROW (EXECUTE PROCEDURE ins_trig_test1(new.code, new.sname))
END SQL
But, as I said at the outset, using triggers for this is not the best way to go; it is redundant given the table definition.
I've not run any of the SQL or SPL past the server; you'll need to check that the semi-colons are in the right places in the SPL, as SPL is fussy about that.
You can find the syntax for the SQL and SPL statements in the Informix 11.70 Information Centre.

Resources