Thursday, 10 November 2011

Sql Store Procedure Dynamic Query Get Max Record




Dynamic Query Get Max Record Multiple table




CREATE PROC Get_Max_ID_SP (@tblName VARCHAR(50),@ClmnName VARCHAR(50))
AS
BEGIN


DECLARE @Qry NVARCHAR(MAX)
DECLARE @Ret NVARCHAR(MAX)
DECLARE @Param NVARCHAR(MAX)

SELECT @Param='@Val NVARCHAR(MAX) OUTPUT'
SELECT @Qry='SELECT @Val =ISNULL(MAX('+@ClmnName+'),0)+1 FROM ' + @tblName

    EXEC  SP_EXECUTESQL  @Qry,@Param,@Val=@Ret OUTPUT  
    
    SELECT @Ret as  MaxCust

END

Output :

exec  Get_Max_ID_SP 'DistMast','Distcode'

Result :

MaxCust
5


Reference Link:



  


   

Friendly Learn Translate to Tamil

Smilely

Services

© 2014