Hola un saludo, soy nuevo en esto del SQL y como tal varias cosas no me salen: Incluyendo el siguiente trigger.
Tengo la siguiente tabla:
CREATE TABLE [dbo].[CA_DOCUMENT_ACTORS](
[document_id] [int] NULL,
[user_id] [varchar](8000) COLLATE Modern_Spanish_CI_AS NULL,
[actors] [varchar](8000) COLLATE Modern_Spanish_CI_AS NULL,
[emiterId] [varchar](8000) COLLATE Modern_Spanish_CI_AS NULL,
[process_id] [int] NULL
) ON [PRIMARY]
Necesito q el trigger se active antes de insertar un nuevo registro cuando el registro tenga un ID_document = a uno existente, entonces concatenar el registro entrante con el q ya existe en la tabla
los campos a concatenar van a ser [user_id] actors] [emiterId]
Para lo cual he diseñado el siguiente trigger pero me sale este error al final:
Msg 156, Level 15, State 1, Procedure TR_UPDATE, Line 68
Incorrect syntax near the keyword 'ELSE'.
No se porque!!!
Bueno ademas q necesito una ayudita de como leer el registro q va a entrar en el INSTEAD OF.
MUCHAS GRACIAS POR SU COLABORACION
-----------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER TR_UPDATE
ON CA_DOCUMENT_ACTORS
INSTEAD OF INSERT
AS
BEGIN
declare @docId as int
--AS
declare @user_id as varchar(max)
declare @c_user_id as varchar(max)
declare @last_actor_id as varchar(max)
declare @c_last_actor_id as varchar(max)
declare @emitter_id as varchar(max)
declare @c_emitter_id as varchar(max)
declare @processId as INT
declare @c_processId as varchar(max)
BEGIN
set @c_user_id = ''
set @c_last_actor_id = ''
set @c_emitter_id = ''
set @c_processId = ''
SET NOCOUNT ON;
--select * from CA_DOCUMENT_ACTORS
IF EXISTS (select *
from CA_DOCUMENT_ACTORS
where document_id = @docId)
Declare CURSOR1 cursor for
SELECT user_id, actors, emiterId, process_id FROM CA_DOCUMENT_ACTORS WHERE DOCUMENT_ID=@docId
open CURSOR1
fetch next from CURSOR1
into @user_id, @last_actor_id, @emitter_id, @processId
while @@fetch_status = 0
begin
set @c_user_id = @c_user_id + user_id + ', '
set @c_last_actor_id = @c_last_actor_id + @last_actor_id + ', '
set @c_emitter_id = @c_emitter_id + @emitter_id + ', '
set @c_processId = @processId--cast(@processId as varchar(max))
end
close CURSOR1
deallocate CURSOR1
select @c_user_id, @c_last_actor_id, @c_emitter_id, @c_processId
UPDATE CA_DOCUMENT_ACTORS SET USER_ID=SUBSTRING(@c_user_id,0,LEN(@c_user_id)),
ACTORS =SUBSTRING(@last_actor_id,0,LEN(@last_actor_id)),
EMITERID = SUBSTRING(@c_emitter_id,0,LEN(@c_emitter_id)),
PROCESSID=SUBSTRING(@c_processId,0,LEN(@c_processI d))
WHERE "document_id" = @docId
end
ELSE BEGIN
Insert into CA_DOCUMENT_ACTORS values(@docId, @user_id, @last_actor_id, @emitter_id, @processId)
end
END