当我执行insert a log record to a table
时,我想stored procedure
首先,
我创建了一个包含3列名为
的表ID(自动增量),消息(VARCHAR),CREATED_ON(DATETIME)
其次我写了一个程序:
create PROCEDURE DO_LOG(@MSG_IN VARCHAR)
as
BEGIN
INSERT INTO test.dbo.TB_ETLLOG(MESSAGE,CREATED_ON)
VALUES(@MSG_IN,GETDATE());
END;
我想在执行另一个程序时写日志,所以我使用这个
EXEC DO_LOG @MSG_IN ='Start DO_ACCOUNT_AML'
但它没有写日志到表。我的代码中有错误吗?感谢
答案 0 :(得分:0)
以下是创建日志存储过程的方法,可以将其添加到代码中:
创建表格:
CREATE TABLE [dbo].[Log](
[LogID] [int] IDENTITY(1,1) NOT NULL,
[LogTime] [datetime] NOT NULL,
[LogAction] [varchar](50) NOT NULL,
[LogNote] [varchar](max) NOT NULL,
CONSTRAINT [PK_CourionLog] PRIMARY KEY CLUSTERED
(
[LogID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
创建存储过程:
CREATE PROCEDURE [dbo].[AddLog]
@LogAction varchar(50),
@LogNote varchar(max),
@LogTime datetime = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
if @LogTime is null
set @LogTime = getdate();
INSERT INTO Log(LogTime, LogAction, LogNote)
Select @Logtime, @LogAction, @LogNote
END
用法:
DECLARE @msg VARCHAR(1000);
DECLARE @tmpCount INT;
SET @tmpCount = 1
SET @msg = 'Completed LastName Check: ' + cast(@tmpCount as varchar(10))
EXECUTE [dbo].[AddLog] 'INFO', @msg
EXECUTE [dbo].[AddLog] 'ERROR', 'This is an error message.'
查看日志消息:
select * from [dbo].[Log]