Avatar of Khan Consultoria
Khan ConsultoriaFlag for Brazil

asked on 

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

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
Microsoft SQL Server

Avatar of undefined
Last Comment
Khan Consultoria
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Khan Consultoria

ASKER

Thanks a lot for your help
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo