Goal: Run a create trigger script using a string inside C #.
Problem 1: Table not found (because the script does not indicate in which Table to create the trigger).
Attempt2: Start the string with "Create Trigger on [dbo]. [Table]".
Problem 2: For Trigger creation SQL does not allow pre-naming the Bank.
Attempt3: Start the string with "Use [Bank] GO Create Trigger ... "
Problem 3: The "Create Trigger" command should always be the first one in the script. In addition, it can not interpret the GO command because 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? Thankful.
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);