Execute stored procedure in a trigger - sql

I do not know if its possible... if not give any idea to achieve this situation.
Let me explain what I want to do:
I want to fire trigger after insert to execute procedure then this procedure will read data from selected table name by user and store it into another table.
First I have an application that reads 2 values from user something like:
Read_From For_Month
And Read_From contains 3 values
(1) Read From Employees
(2) Read From Entity
(3) Read From Salaries
And For_Month the user select a date
Employees table:
Emp_ID EMP_NAME EMP_SHARE FOR_MONTH
---------------------------------------
121 Salim 25.8 01/06/2015
I want to fire trigger after click add button in my application.
So, this trigger will store entered values by user and pass it to procedure for execution, then the procedure will be execute based on these values to read from which table? And for which month?
If it's possible I need this trigger and procedure guys

I'm not sure I fully understand your needs, but here is what I think...
Here, I'm assuming that you have your client (Web) application, some middleware/server-side logic (say a SERVLET) that is approached by your application, and the DB behind the middleware.
First, you don't trigger things within the DB by clicking on a button of your Web application. What you do, is to invoke a stored procedure that performs some actions.
You can proceed in one of two ways:
The stored procedure you invoke when clicking on the button performs the insert and invokes the additional logic that needs to run using the inserted data
You create a trigger on the table into which the data needs to be inserted, where this trigger invokes the logic that needs to be executed using the inserted data. The stored procedure you invoke when clicking on the button will just insert the new data (and, due to the trigger, the additional logic will be executed).
Hope this is what you were looking for.

Related

Stored procedure to run a query based off timestamp in table

I'm trying to write a procedure where the query will only run if the table has been updated at a certain time.
There is actually a timestamp in the table so I just need to check something like
IF table.update = GETDATE() THEN …
I'm not even sure where to start here.
Can anyone point me in the direction of a place where I can learn about stuff like this, or show me which functions I need to use?
You need to start by looking at how to use Database JOBs.
Since no stored procedure can start by itself.
Inside the Job, you can define how often and when stored procedure run.
The code inside the job can be simple SQL.
OR
You can create an update Trigger on the table. And inside the trigger base on the time call the stored procedure.
Update:
Just saw your latest comment. I have a similar app in my company. We use a Database Job to send a batch of auotmated emails on specific time everyday.
You need a trigger, in the UPDATE method, and you can have it run when it is saved in a separate table.
The new table must have the primary key and the time stamp with the GETDATE () method.
To Implement the Trigger:
create trigger trTriggerTest on tableName after update
As
Begin
set nocount on;
INSERT INTO TimeRegister(Id, date, ...) VALUES (#Id, GetDate(), ...);
end
go
To register en new table:
INSERT INTO TimeRegister(Id, date, ...) VALUES (#Id, GetDate(), ...);
Documentation: Trigger

Oracle triggers and stored procedures

I need to make for my webApp a trigger to execute a stored procedure on Oracle. But i'm very new to Oracle and I'm still getting the hang of it. I can make a simple Trigger with a sequence to auto-increment a value from a table, but that's it.
Is there any good tutorials and examples available on this specific subject? I tried searching here, but i have only found a very generic question: How can i learn Stored Procedure and Trigger?. But i can be more specific: I need this trigger to run a stored procedure that generates a new code for my user, adding data to this code. The procedure is done, i just don't know how to use it in a trigger, pass the parameters, and how to insert/update values from the oracle trigger itself.
Help will be much appreciated.
Assuming your function to generate the code is named f_generate_code() and your table is named foobar and the column that should be populated is name code you'd do it like this:
create or replace trigger trg_update_code
before insert or update on foobar
for each row
begin
:new.code := f_generate_code();
end;
/

SQL Differences between stored procedure and triggers

I'm having trouble understanding the difference between a stored procedure and a trigger in sql.
If someone could be kind enough to explain it to me that would be great.
A stored procedure is a user defined piece of code written in the local version of PL/SQL, which may return a value (making it a function) that is invoked by calling it explicitly.
A trigger is a stored procedure that runs automatically when various events happen (eg update, insert, delete).
IMHO stored procedures are to be avoided unless absolutely required.
Think of a stored procedure like a method in an object-oriented programming language. You pass in some parameters, it does work, and it can return something.
Triggers are more like event handlers in an object-oriented programming language. Upon a certain condition, it can either (a) handle the event itself, or (b) do some processing and allow for the event to continue to bubble up.
In respect to triggers in SQL Server: a trigger is a special piece of code that automatically gets executed when an event occurs in the database server.
DML triggers execute when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view. These triggers fire when any valid event is fired, regardless of whether or not any table rows are affected
We can create trigger like this:
CREATE TRIGGER TriggerName
ON [dbo].[TableName]
FOR DELETE, INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
END
A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure.
We can do lot of programming stuff in a stored procedure and execute again and again.
We can create procedure which take the input process and give the output
We can handle the error through try catch
Stored procedures can be nest and call again and again with nested calling
It's more secure
We can create a stored procedure like this:
CREATE PROCEDURE dbo.Sample_Procedure
#param1 int = 0,
#param2 int
AS
SELECT #param1,#param2
RETURN 0;
Differences in both of then
Trigger can not be called manually where stored procedure can be called manually.
Trigger executes automatically when event happens and can be use for reporting and data protection from deleting or dropping the table and data from database. We can prevent from trigger. On the other hand, a stored procedure has to be called by somebody.
A stored procedure can be called from front end (client application) but trigger can not be called from client application.
Some differences between triggers and procedures:
We can execute a stored procedure whenever we want with the help of the exec command, but a trigger can only be executed whenever an event (insert, delete, and update) is fired on the table on which the trigger is defined.
Stored procedure can take input parameters, but we can't pass parameters as input to a trigger.
Stored procedures can return values but a trigger cannot return a value.
We can use transaction statements like begin transaction, commit transaction, and rollback inside a stored procedure but we can't use transaction statements inside a trigger
We can call a stored procedure from the front end (.asp files, .aspx files, .ascx files, etc.) but we can't call a trigger from these files.
A trigger fires after an insert, update, or delete. A stored procedure is a server-side program that is run when you invoke it.
A stored procedure is a group of SQL statements that is compiled one time, and then can be executed many times. Triggers are named database objects that are implicitly fired when a triggering event occurs. The trigger action can be run before or after the triggering event. Triggers are similar to stored procedures but differ in the way that they are invoked. A trigger is not called directly by a user, where as a stored procedure is directly called by a user.
A stored procedure is a piece of code that resides in and is executed by the DBMS and can be called explicitly by the client or by other stored procedures. It is usually written in a procedural extension of SQL, such as PL/SQL under Oracle or T-SQL under MS SQL Server, but some DBMSes support more general languages such as Java or .NET as well.
A trigger is a (sort of) stored procedure that cannot be called explicitly, and instead executes automatically in response to events such as insertion, update or deletion or rows in a table.
A trigger is a special kind of stored procedure. It is attached to a table and only triggers when an insert, update or delete occurs. Stored procedures are essential functions that you can create and reuse in the table.
A stored procedure can be called form another stored procedure but not ab trigger.
A stored procedure can be executed whenever a user wants but not a trigger.A trigger is fired only when events occur.
A stored procedure can have a print statement,multiple parameters and return values but not a trigger.
A stored procedure can be called from front end but not trigger.
***TRIGGERS***
Action on specific time.
Triggers is a special type of stored procedure that is not called directly by user.
When the trigger is created, it is defined to fire when a specific type of data modification is made against a specific table or column
If you are familiar with JavaScript, a trigger is an addEventListener and Stored Procedure is a callback.
Both are database objects containing blocks lof code that can be used for implementing business logic
The differences are:
1) Triggers fire automatically but they need events for that.
(Example: create,alter,drop,insert,delete,update) .
2) Procedures have to be explicitly called and then executed.
They do not need create,alter,drop,insert,delete,update.
we can also execute procedures automatically using the sp_procoption.
3) we cannot pass parameters inside the triggers,
but we can pass parameters inside stored procedures
example: if we want to display a message "error"
using a trigger: we need some DDL/DML Statement
using a procedure: NO DDL/DML is needed
Difference Between a Stored Procedure and a Trigger
We can define a trigger as a database object just like a stored procedure, or we can say it is a special kind of stored procedure which fires when an event occurs in a database. We can execute a SQL query that will "do something" in a database when an event is fired.
Triggers are fired implicitly while stored procedures are fired explicitly.

SQL: Using Stored Procedure within a Stored Procedure

I have a few stored procedures that return the same set of data (same columns) to a user. The stored procedure called depends on certain conditions. These stored procedures are fairly intensive and are being run by every user of the system. I would like to create stored procedure that calls each of these procedures and stores the data on a separate table. I will then run this new stored procedure every 5 minutes or so and let the users pull from the new table.
T_OutboundCallList is a permanent table with the same columns as returned by the two stored procedures.
I would like something like the following but when I try to run this it just runs continuously and I have to stop the procedure.
BEGIN
TRUNCATE TABLE T_OutboundCallList
INSERT T_OutboundCallList EXECUTE p_LeadVendor_GetCallsForCallList
INSERT T_OutboundCallList EXECUTE p_CallLog_GetAbandonedCallsCallList
END
Each of the procedures (*CallList) return a list of calls to be made and I do want them entered into the new table in this order (LeadVendor calls before AbandonedCalls). I also need to clear the table before adding the calls as there may be new calls that need to be higher in the list.
Is there some problem with this procedure that I am not seeing?
Thanks,
Brian
Without seeing the code in your *CallList procs it is hard to say what issue you are having. You should have the insert commands inside of your nested procedure. You can use the results of a procedure to insert data, but not like you are above. It is using OPENROWSET, and I think you will be better off the way I suggested.

Using Same Stored Procedure for Both Insert and Update in Entity Framework

I have a stored procedure that does both the insert and the update in one fell swoop (if the id == 0 then it's an insert, otherwise, update). I'd love to use this for both the insert and the update methods in Entity Framework, but this isn't looking feasible. Am I correct that I'll have to split the methods into two different stored procedures, or is there a way around this?
On way I would try first, is opening the EDMX in the XML editor and finding the sproc (Function) in the StorageModel element, copy it, giving it a new name.
Then you can map one actual stored procedure twice once as the Insert Modification function once as the Update Modification function.
I haven't actually tried this, but I can't see why this won't work.
Hope this helps
Alex James, Program Manager, Microsoft