Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help converting from an Insert sproc to trigr

Author  Topic 

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-09 : 22:37:31
Sproc:
USE [Bss]
GO
/****** Object: StoredProcedure [dbo].[spInsertLog] Script Date: 03/10/2009 04:12:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spInsertLog]
(
@LogType int = 1,
@Title varchar(128),
@Note varchar(512),
@Employee int,
@Job int,
@Permit int,
@Customer int,
@Professional int,
@Document int
)
AS
SET NOCOUNT OFF;
IF ((SELECT [Enabled] FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType)) = 1)
BEGIN
IF (ISNULL(@Title, '') = '')
SELECT @Title = Title FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType)

INSERT INTO [Log](LogType, Title, Note, Employee, Job, Permit, Customer, Professional, [Document])
VALUES (@LogType,@Title,@Note,@Employee,@Job,@Permit,@Customer,@Professional,@Document)
SELECT LogId, Title, Note, Employee, Job, Permit, Customer, Professional, [Document], CreationTime FROM [Log] WHERE (LogId = SCOPE_IDENTITY())
END
ELSE
BEGIN
(SELECT @Title = Title FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType))
PRINT 'LogType ' + CAST(@LogType AS varchar(5)) + ' (' + @Title + ') is disabled, record was not added.'
SELECT LogId = NULL, Title = NULL, Note = NULL, Employee = NULL, Job = NULL, Permit = NULL, Customer = NULL, Professional = NULL, [Document] = NULL, CreationTime = NULL
END


I want this SPROC to be the only way to add data to the log table so I decided to put it all in an instead of insert trigger, but I don't manage to, I get an error:

Trigger:
CREATE TRIGGER Log_INSERT
ON [Log]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT OFF;

DECLARE
@LogType int,
@Title varchar(128),
@Note varchar(512),
@Employee int,
@Job int,
@Permit int,
@Customer int,
@Professional int,
@Document int

SELECT
@LogType = LogType,
@Title = Title,
@Note= Note,
@Employee = Employee,
@Job = Job,
@Permit = Permit,
@Customer = Customer,
@Professional = Professional,
@Document = Document
FROM inserted

IF ((SELECT [Enabled] FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType)) = 1)
BEGIN
IF (ISNULL(@Title, '') = '')
SELECT @Title = Title FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType)

INSERT INTO [Log](LogType, Title, Note, Employee, Job, Permit, Customer, Professional, [Document])
VALUES (@LogType,@Title,@Note,@Employee,@Job,@Permit,@Customer,@Professional,@Document)
SELECT LogId, Title, Note, Employee, Job, Permit, Customer, Professional, [Document], CreationTime FROM [Log] WHERE (LogId = SCOPE_IDENTITY())
END
ELSE
BEGIN
(SELECT @Title = Title FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType))
PRINT 'LogType ' + CAST(@LogType AS varchar(5)) + ' (' + @Title + ') is disabled, record was not added.'
SELECT LogId = NULL, Title = NULL, Note = NULL, Employee = NULL, Job = NULL, Permit = NULL, Customer = NULL, Professional = NULL, [Document] = NULL, CreationTime = NULL
END
GO


I get the following error:
Msg 102, Level 15, State 1, Procedure Log_INSERT, Line 44
Incorrect syntax near 'END'.


Shimmy

guptam
Posting Yak Master

161 Posts

Posted - 2009-03-09 : 23:10:23
1) Your missing "END" before the "GO" statement to close of the Trigger.

2) Change (SELECT @Title = Title FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType)) to SELECT @Title = Title FROM [BSS].[dbo].[LogType] WHERE (LogTypeId = @LogType). Don't need brackets around it unless you are making another if statement.

Your error is most likely #1, SQL will complain like mad man .. but will not tell you it wants another "END" LOL.

--
Mohit K. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-09 : 23:15:05
quote:
Originally posted by guptam
Your error is most likely #1, SQL will complain like mad man .. but will not tell you it wants another "END" LOL.



Oh yeah!
that helped
your good thanks



Shimmy
Go to Top of Page
   

- Advertisement -