Example of procedure running an dynamic SQL based on procedure's parameter:
CREATE PROCEDURE [dbo].[SP_DYNAMIC_SQL_TEST]
(@V_PARAMETER INT)
AS
BEGIN
SET NOCOUNT ON
DECLARE @delete_sql nvarchar(1000)
DECLARE @ParmDefinitionDel NVARCHAR(500)
SET @delete_sql = 'DELETE TB_DYNAMIC_SQL'
IF (@V_PARAMETER IS NOT NULL)
SET @delete_sql = @delete_sql + ' WHERE COLUMN_NAME = @V_PARAMETER_DYN'
SET @ParmDefinitionDel = N'@V_PARAMETER_DYN INT'
PRINT @delete_sql
EXEC sp_executesql @delete_sql, @ParmDefinitionDel, @V_PARAMETER_DYN = @V_PARAMETER
END
CREATE PROCEDURE [dbo].[SP_DYNAMIC_SQL_TEST]
(@V_PARAMETER INT)
AS
BEGIN
SET NOCOUNT ON
DECLARE @delete_sql nvarchar(1000)
DECLARE @ParmDefinitionDel NVARCHAR(500)
SET @delete_sql = 'DELETE TB_DYNAMIC_SQL'
IF (@V_PARAMETER IS NOT NULL)
SET @delete_sql = @delete_sql + ' WHERE COLUMN_NAME = @V_PARAMETER_DYN'
SET @ParmDefinitionDel = N'@V_PARAMETER_DYN INT'
PRINT @delete_sql
EXEC sp_executesql @delete_sql, @ParmDefinitionDel, @V_PARAMETER_DYN = @V_PARAMETER
END
No comments:
Post a Comment