0
Goal: Run a create Trigger script using a string within C#.
Problem1: Table not found (because the script does not indicate in which Table to create Trigger).
Tentative2: Start string with "Create Trigger on [Bank]. [dbo]. [Table]".
Problema2: For Trigger creation SQL does not allow to pre-name the Database.
Tentative3: Start string with " Use [Bank] GO Create Trigger..."
Problema3: The "Create Trigger" command must always be the first one in the script. Also it cannot interpret the GO command, as it is not native to SQL.
I have no other option, I tried to do via stored Procedure, the same problems occur. Please, has anyone ever faced this problem before? Grateful.
strSql = " CREATE TRIGGER [dbo].[TGR_ALERTA] ";
strSql += " ON [dbo].[Alertas_Integracao] ";
strSql += " after INSERT ";
strSql += " AS ";
strSql += " If (SELECT Defeito FROM INSERTED) = 1 ";
strSql += " begin ";
strSql += " BEGIN ";
strSql += " DECLARE ";
strSql += " @EQUIPAMENTO_ID int, ";
strSql += " @SETOR_ID int, ";
strSql += " @PROBLEMA int, ";
strSql += " @INTEQUIPAMENTO int, ";
strSql += " @STRALERTA varchar(max), ";
strSql += " @IDEVENTO int ";
strSql += " select @INTEQUIPAMENTO = Equipamento from Inserted ";
strSql += " select @STRALERTA = Alerta from Inserted ";
strSql += " select @EQUIPAMENTO_ID = (SELECT ieq_equ_equipamentos_id ";
strSql += " from EQU_IEQ_INTEGRACAO_EQUIPAMENTOS ";
strSql += " where ieq_equipamento_integrado = @INTEQUIPAMENTO) ";
strSql += " select @SETOR_ID = (Select equ_sto_setores_id ";
strSql += " from EQU_EQU_EQUIPAMENTOS ";
strSql += " where equ_equipamentos_id = @EQUIPAMENTO_ID) ";
strSql += " select @PROBLEMA = (Select prb_problemas_id ";
strSql += " from EVE_PRB_PROBLEMAS ";
strSql += " where prb_descricao = @STRALERTA ";
strSql += " and prb_equ_equipamentos_id = @EQUIPAMENTO_ID) ";
strSql += " insert into EVE_EVE_EVENTOS ";
strSql += " (eve_usu_usuarios_id, ";
strSql += " eve_sto_setores_id, ";
strSql += " eve_sta_status_id, ";
strSql += " eve_data_inclusao, ";
strSql += " eve_problema, ";
strSql += " eve_corretiva, ";
strSql += " eve_equ_equipamentos_id, ";
strSql += " eve_inativo, ";
strSql += " eve_prb_problemas_id) ";
strSql += " values ";
strSql += " (2, ";
strSql += " @SETOR_ID, ";
strSql += " 6, ";
strSql += " GETDATE(), ";
strSql += " @STRALERTA, ";
strSql += " 1, ";
strSql += " @EQUIPAMENTO_ID, ";
strSql += " 0, ";
strSql += " @PROBLEMA ";
strSql += " ) ";
strSql += " SELECT @IDEVENTO = @@IDENTITY ";
strSql += " insert into EVE_SEV_STATUS_EVENTOS ";
strSql += " values (@IDEVENTO, 6, getdate()) ";
strSql += " END ";
strSql += " end ";
clsCon.ExecutarSql(strSql);
Put the sources in question!
– Rodolpho Sa