USE of COALESCE in a Stored Procedure MySQL

use of coalesce

USE [DB_Name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_FillMarks]
	@studentId bigint
AS
BEGIN
	SET NOCOUNT ON;
	SELECT ID, Name, Marks
  FROM t_Results r
 where STUDENTID =  COALESCE(NULLIF(@studentId, '0'), STUDENTID)
 and Marks >= 80
END

Explanation

This will select details of student whose id is passed to the procedure if the id matches with that in the DB and his/her marks are greater or equal to 80.

NULLIF(@studentId, ‘0’) will return the first value that matches with the student id.

COALESCE will ensure to return first non-null value.

One thought on “USE of COALESCE in a Stored Procedure MySQL

Add yours

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started