Stored procedure that Automatically delete rows older than 7 days in MYSQL

Mysql has its EVENT functionality for avoiding complicated cron interactions when much of what you are scheduling is sql related, and less file related. See the Manual page here. Hopefully the below reads as a quick overview of the important steps and things to consider, and verifiable testing too. show variables where variable_name=”event_scheduler”; +—————–+——-+ | … Read more

How to call a stored procedure from Java and JPA

JPA 2.1 now support Stored Procedure, read the Java doc here. Example: StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery(“sales_tax”); // set parameters storedProcedure.registerStoredProcedureParameter(“subtotal”, Double.class, ParameterMode.IN); storedProcedure.registerStoredProcedureParameter(“tax”, Double.class, ParameterMode.OUT); storedProcedure.setParameter(“subtotal”, 1f); // execute SP storedProcedure.execute(); // get result Double tax = (Double)storedProcedure.getOutputParameterValue(“tax”); See detailed example here.

Using stored procedure output parameters in C#

I slightly modified your stored procedure (to use SCOPE_IDENTITY) and it looks like this: CREATE PROCEDURE usp_InsertContract @ContractNumber varchar(7), @NewId int OUTPUT AS BEGIN INSERT INTO [dbo].[Contracts] (ContractNumber) VALUES (@ContractNumber) SELECT @NewId = SCOPE_IDENTITY() END I tried this and it works just fine (with that modified stored procedure): // define connection and command, in using … Read more

Nested stored procedures containing TRY CATCH ROLLBACK pattern?

This is our template (error logging removed) This is designed to handle Paul Randal’s article “No such thing as a nested transaction in SQL Server” Error 266 Trigger Rollbacks Explanations: all TXN begin and commit/rollbacks must be paired so that @@TRANCOUNT is the same on entry and exit mismatches of @@TRANCOUNT cause error 266 because … Read more

Entity Framework Stored Procedure Table Value Parameter

UPDATE I’ve added support for this on Nuget Package – https://github.com/Fodsuk/EntityFrameworkExtras#nuget (EF4,EF5,EF6) Check out the GitHub repository for code examples. Slightly off question, but none the less useful for people trying to pass user-defined tables into a stored procedure. After playing around with Nick’s example and other Stackoverflow posts, I came up with this: class … Read more

Difference between language sql and language plpgsql in PostgreSQL functions

SQL functions … are the better choice: For simple scalar queries. Not much to plan, better save any overhead. For single (or very few) calls per session. Nothing to gain from plan caching via prepared statements that PL/pgSQL has to offer. See below. If they are typically called in the context of bigger queries and … Read more