How to perform a procedure with each insert?

1

When I am going to make a registration I want to, according to the selected category, record an id of the moderator, in the question table.

select idcategoria from tb_pergunta
select idmoderador,idcategoria from tb_moderador


USE [BANCOTESTE]
GO
/****** Object:  StoredProcedure [dbo].[ATUALIZAMODERADOR]    Script Date: 06/15/2015 13:14:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ATUALIZAMODERADOR]
 @IDPERGUNTA INT 
AS
BEGIN
 DECLARE @IDCATEGORIA INT
 DECLARE @IDMODERADOR INT

 SELECT  @IDCATEGORIA = IDCATEGORIA FROM TB_PERGUNTA WHERE IDPERGUNTA = @IDPERGUNTA
 SELECT  @IDMODERADOR = IDMODERADOR FROM TB_MODERADOR WHERE IDCATEGORIA = @IDCATEGORIA
 UPDATE TB_PERGUNTA SET  IDMODERADOR = @IDMODERADOR, BLOQUEIO_PERGUNTA='N', EXCLUIR_PERGUNTA='N'  WHERE IDPERGUNTA = @IDPERGUNTA

END
    
asked by anonymous 15.06.2015 / 18:32

1 answer

0

The solution would be to create a triggers

The creation would look like this:

insert into tb_quest values ('1', '1', '', 'title question', 'description', '', '', '')

USE [BANCOTESTE]
GO
/****** Object:  Trigger [dbo].[TGR_TB_PERGUNTA]    Script Date: 06/15/2015 15:11:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TGR_TB_PERGUNTA]
 ON [dbo].[TB_PERGUNTA]
 FOR INSERT
AS
BEGIN
   DECLARE @IDCATEGORIA INT
   DECLARE @IDMODERADOR INT

   SELECT  @IDCATEGORIA = IDCATEGORIA FROM INSERTED
   SELECT  @IDMODERADOR = IDMODERADOR FROM TB_MODERADOR WHERE IDCATEGORIA = @IDCATEGORIA
   UPDATE TB_PERGUNTA SET  IDMODERADOR = @IDMODERADOR, BLOQUEIO_PERGUNTA='N', EXCLUIR_PERGUNTA='N'  
END
    
15.06.2015 / 20:44