Here is the code below;
SET @QUERY = '
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
--
CREATE PROCEDURE [dbo].[sp_sp_duration_create_sp_duration_data_log]
@sp_name varchar(100),
@duration int,
@details varchar(250)
AS
BEGIN
IF @duration > 100
Begin
INSERT INTO [sp_duration_logs]
([sp_name]
,[duration]
,[details])
VALUES
(@sp_name
,@duration
,@details)
END
END
';
EXECUTE('USE ['+@DBNAME+']; '+@QUERY);
When I try to execute this t-sql command , I get an error:
Msg 111, Level 15, State 1, Procedure sp_sp_duration_create_sp_duration_data_log, Line 16 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
I think I need to use 'GO' in query , but when I use GO another error occurring. (Invalid object named 'GO' or something else).