How to Pass Columns Name and Table Name as Parameter to Stored Procedure in MySQL

Last Reply on Nov 11, 2016 06:53 AM By dharmendr

Posted on Nov 11, 2016 03:59 AM

Currently i am Creating Some Stored Procedure in mysql, but i would like to Use Some Dynamic Stored Procedure, means i would like to pass Parameters in mysql Stored Procedure.

 

I request to you how to Pass Columns name and Table name as Paramaters.

You are viewing reply posted by: dharmendr on Nov 11, 2016 06:53 AM.
Posted on Nov 11, 2016 06:53 AM

Hi sandip.AP,

Check with the below query. This is with SQL Server. So you need to change as per mysql query.

DECLARE @Date DATETIME, @Col_Name VARCHAR(MAX), @Table_Name VARCHAR(50)
SET @Date = CurDate()
SET @Col_Name = 'AJB1_STR1'
SET @Table_Name = 'k01_block1_ajb1'

DECLARE @DynamicQuery NVARCHAR(MAX)
SET @DynamicQuery = 'SELECT	' + @Col_Name +' FROM ' + @Table_Name + ' WHERE Date(LocalCol)= ' + @Date

EXEC(@DynamicQuery)