We help IT Professionals succeed at work.
Get Started

Error in script to run in both mssql version 2014 and 2017

61 Views
Last Modified: 2020-01-08
Hello guys,

I need help with my script.

I have a script that must be run in MSQL 2014 e MSSQ2017

The problem is the syntax of error, how to work around with this?

the error:

Msg 102, Level 15, State 1, Procedure td_ensespecialidades, Line 64
Sintaxe incorreta próxima a '@errno'.

if exists (select name from sysobjects where name = 'td_ensespecialidades') 
  drop trigger td_ensespecialidades;
go


exec('create trigger td_ensespecialidades on ensEspecialidades for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  DeleteParentRestrict: Impossivel excluir pai "ensEspecialidades" pois existem registros na filha "ensGrades_Profissionais"  */
    if exists (select 1 from ensGrades_Profissionais t2, deleted t1
               where  t2.especialidade = t1.especialidade)
       begin
          select @errno  = 50006,
                 @errmsg = ''Existem registros em "ensGrades_Profissionais". Impossível excluir "ensEspecialidades".''
          goto error
       end

    /*  DeleteParentCascade: apaga todas as filhas "ensPolos_Especialidades"  da Pai "ensEspecialidades"  */
    set nocount on

    delete ensPolos_Especialidades
      from   ensPolos_Especialidades t2, deleted t1
      where  t2.especialidade = t1.especialidade

    set nocount off

    /*  DeleteParentCascade: apaga todas as filhas "ensEspecialidades_Profissionais"  da Pai "ensEspecialidades"  */
    set nocount on

    delete ensEspecialidades_Profissionais
      from   ensEspecialidades_Profissionais t2, deleted t1
      where  t2.especialidade = t1.especialidade

    set nocount off

    /*  DeleteParentCascade: apaga todas as filhas "ensRegioesAdm_ValoresPadrao"  da Pai "ensEspecialidades"  */
    set nocount on

    delete ensRegioesAdm_ValoresPadrao
      from   ensRegioesAdm_ValoresPadrao t2, deleted t1
      where  t2.especialidade = t1.especialidade

    set nocount off

    /*  DeleteParentSetNull: Exclusão de registro na pai "ensEspecialidades" deixa null a chave na filha "ensProfissionais"  */
    set nocount on

    update ensProfissionais
     set   especialidade_principal = NULL
    from   ensProfissionais t2, deleted t1
    where  t2.especialidade_principal = t1.especialidade

    set nocount off

    return

error:
    raiserror @errno @errmsg
    rollback  transaction
end
')
----------------------------------------

Open in new window


Thanks a lot for help
Alex
Comment
Watch Question
SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE