error in query

  • What is the DDL for the table FicheirosImportar, and sincronismo?

    Jared
    CE - Microsoft

  • Hi

    Here is the DDL

    :CREATE TABLE [dbo].[FicheirosImportar](

    [SessaoInicio] [varchar](5) NULL,

    [SessaoFim] [varchar](5) NULL,

    [Codrf] [varchar](5) NULL,

    [Nome] [varchar](500) NULL,

    [Caminho] [varchar](500) NULL,

    [Valido] [bit] NULL,

    [Ordem] [int] NULL

    ) ON [PRIMARY]

  • CREATE TABLE [dbo].[SINCRONISMO](

    [CODIGO] [bigint] IDENTITY(1,1) NOT NULL,

    [DT_INI_PROC] [datetime] NOT NULL,

    [DT_FIM_PROC] [datetime] NULL,

    [TP_PROCESSO] [char](1) NOT NULL,

    [COD_TP_CLASSIFICACAO] [int] NOT NULL,

    [UTILIZADOR] [int] NOT NULL,

    [COD_SINCRONISMO] [bigint] NULL,

    [FICHEIRO] [varchar](255) NOT NULL,

    [COD_REP_FISCAL] [varchar](5) NULL,

    [COD_SINCRONISMO_INI] [bigint] NULL,

    [COD_SINCRONISMO_FIM] [bigint] NULL,

    [STATUS] [int] NOT NULL,

    CONSTRAINT [PK_SINCRONISMO] PRIMARY KEY CLUSTERED

    (

    [CODIGO] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

  • Ok, let's start with...

    1. Why is this "delete from FicheirosImportar where sessaoinicio <> sessaofim and sessaoinicio <> 'T'" in the cursor?

    2. @codrf should be varchar(5), not varchar(10)

    But, your error is coming from the fact that @maxVal is a VARCHAR(100), yet the value it is being set to from cod_sincronismo_fim is a BIGINT.

    Jared
    CE - Microsoft

  • thank you for the feedback.

    I have tried to convert the bigint field to varchar, but still same error happen.

    Here is the code:

    ALTER PROCEDURE [dbo].[spSGCT_VALIDA_SGCTLocais] (@DB as varchar(50))

    AS

    BEGIN

    set nocount on

    delete from FicheirosImportar where sessaoinicio <> sessaofim and sessaoinicio <> 'T'

    DECLARE @MaxTotalidade as varchar(100),

    @MaxDiferenca as bigint,

    @Inicio as varchar(10),

    @fim as varchar(10),

    @CODRF as varchar(10),

    @SQLString NVARCHAR(500),

    @ParmDefinition NVARCHAR(500)

    DECLARE db_cursor CURSOR FOR

    select distinct(codrf) from FicheirosImportar

    OPEN db_cursor

    FETCH NEXT FROM db_cursor INTO @codrf

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @SQLString = 'select @maxVal = isnull(max(convert(varchar(100),cod_sincronismo_fim)),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =2

    and cod_rep_fiscal ='+@codrf+''

    SET @ParmDefinition = '@maxVal nvarchar(10) OUTPUT'

    EXECUTE sp_executesql @SQLString, @ParmDefinition, @maxVal = @MaxTotalidade OUTPUT

    SELECT @MaxTotalidade

    -- exec('set '+@MaxTotalidade+ ' =(select convert(bigint,isnull(max(cod_sincronismo_fim),0))

    -- from SGCTCentral.dbo.sincronismo where

    -- tp_processo =''I'' and cod_tp_classificacao =2 and cod_rep_fiscal =''4.01'')')

    --

    -- exec('set '+@MaxTotalidade+' =(select convert(bigint,isnull(max(cod_sincronismo_fim),0))

    -- from SGCTCentral.dbo.sincronismo where

    -- tp_processo =''I'' and cod_tp_classificacao =2 and cod_rep_fiscal =''4.01'')')

    ---- exec('set '+@MaxTotalidade+' = (select convert(bigint,isnull(max(cod_sincronismo_fim),0)) from '+@DB+'.dbo.sincronismo where

    ---- tp_processo =''I'' and cod_tp_classificacao =2 and cod_rep_fiscal ='''+@codrf+'''')

    --

    -- exec('set '+@MaxDiferenca+' = (select isnull(max(cod_sincronismo_fim),0) from ' +@DB+'.dbo.sincronismo where

    -- tp_processo =''I'' and cod_tp_classificacao =1 and cod_rep_fiscal ='''+@codrf+'''')

    -- if (select count(sessaofim) from FicheirosImportar where

    -- sessaoinicio ='T' and sessaofim > @MaxTotalidade

    -- and sessaofim > @MaxDiferenca and codrf =''+@codrf+'') <> 0

    --

    -- BEGIN

    --

    -- set @MaxTotalidade = (select isnull(max(sessaofim),0) from FicheirosImportar where

    -- sessaoinicio ='T' and sessaofim > @MaxTotalidade

    -- and sessaofim > @MaxDiferenca and codrf =''+@codrf+'')

    --

    --

    -- update FicheirosImportar set valido =1 where codrf = ''+@codrf+''

    -- and sessaoinicio ='T' and sessaofim = @MaxTotalidade

    --

    -- END

    --

    --

    --

    -- if (@MaxTotalidade > @MaxDiferenca)

    -- BEGIN

    --

    -- if (select count(sessaofim) from FicheirosImportar where

    -- sessaoinicio <> 'T' and sessaofim > @MaxTotalidade and codrf =''+@codrf+'') <> 0

    --

    --

    --

    -- update FicheirosImportar set valido =1 where

    -- sessaoinicio <> 'T' and sessaofim > @MaxTotalidade and codrf =''+@codrf+''

    --

    --

    --

    --

    -- END

    -- ELSE

    --

    --

    -- if ('select count(sessaofim) from FicheirosImportar where

    -- sessaoinicio <> ''T'' and sessaofim > @MaxTotalidade and codrf ='''+@codrf+'''

    -- and (sessaofim > '+@MaxDiferenca+' or sessaofim not in

    -- (select cod_sincronismo_fim from '+@db+'.dbo.sincronismo

    -- where cod_rep_fiscal ='''+@codrf+''' and cod_tp_classificacao =1

    -- and cod_sincronismo_fim > '+@MaxTotalidade+'))')<> 0

    --

    --

    -- BEGIN

    --

    --

    -- exec('update FicheirosImportar set valido =1 where

    -- sessaoinicio <> ''T'' and sessaofim > '+@MaxTotalidade+' and codrf ='''+@codrf+'''

    -- and (sessaofim > '+@MaxDiferenca+' or sessaofim not in(

    -- select cod_sincronismo_fim from '+@db+'.dbo.sincronismo

    -- where cod_rep_fiscal ='''+@codrf+''' and cod_tp_classificacao =1

    -- and cod_sincronismo_fim > '+@MaxTotalidade+'))')

    --

    --

    --

    --

    -- END

    --

    --

    --

    --

    -- update FicheirosImportar set ordem =1 where valido =1 and

    -- sessaoinicio ='T' and codrf =''+@codrf+''

    --

    --

    --

    -- update ficheirosimportar set ordem = a.row +1

    -- from

    -- (select ROW_NUMBER() OVER( ORDER BY sessaoinicio) AS Row,codrf RF,

    -- sessaoinicio SI,sessaofim SF

    -- FROM FicheirosImportar

    -- WHERE sessaoinicio <> 'T' and valido =1) a

    -- where ficheirosimportar.codrf = RF and ficheirosimportar.sessaoinicio = SI

    -- and ficheirosimportar.sessaofim =SF and RF = ''+@codrf+''

    --

    --

    --

    FETCH NEXT FROM db_cursor INTO @codrf

    END

    CLOSE db_cursor

    DEALLOCATE db_cursor

    END

  • and I have done the opposite (put the variable as bigint) but still an error:

    ALTER PROCEDURE [dbo].[spSGCT_VALIDA_SGCTLocais] (@DB as varchar(50))

    AS

    BEGIN

    set nocount on

    delete from FicheirosImportar where sessaoinicio <> sessaofim and sessaoinicio <> 'T'

    DECLARE

    --@MaxTotalidade as varchar(100),

    @MaxTotalidade as bigint,

    @MaxDiferenca as bigint,

    @Inicio as varchar(10),

    @fim as varchar(10),

    @CODRF as varchar(10),

    @SQLString NVARCHAR(500),

    @ParmDefinition NVARCHAR(500)

    DECLARE db_cursor CURSOR FOR

    select distinct(codrf) from FicheirosImportar

    OPEN db_cursor

    FETCH NEXT FROM db_cursor INTO @codrf

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @SQLString = 'select @maxVal = isnull(max(cod_sincronismo_fim),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =2

    and cod_rep_fiscal ='+@codrf+''

    --SET @ParmDefinition = '@maxVal nvarchar(10) OUTPUT'

    SET @ParmDefinition = '@maxVal bigint OUTPUT'

    EXECUTE sp_executesql @SQLString, @ParmDefinition, @maxVal = @MaxTotalidade OUTPUT

    SELECT @MaxTotalidade

    -- exec('set '+@MaxTotalidade+ ' =(select convert(bigint,isnull(max(cod_sincronismo_fim),0))

    -- from SGCTCentral.dbo.sincronismo where

    -- tp_processo =''I'' and cod_tp_classificacao =2 and cod_rep_fiscal =''4.01'')')

    --

    -- exec('set '+@MaxTotalidade+' =(select convert(bigint,isnull(max(cod_sincronismo_fim),0))

    -- from SGCTCentral.dbo.sincronismo where

    -- tp_processo =''I'' and cod_tp_classificacao =2 and cod_rep_fiscal =''4.01'')')

    ---- exec('set '+@MaxTotalidade+' = (select convert(bigint,isnull(max(cod_sincronismo_fim),0)) from '+@DB+'.dbo.sincronismo where

    ---- tp_processo =''I'' and cod_tp_classificacao =2 and cod_rep_fiscal ='''+@codrf+'''')

    --

    -- exec('set '+@MaxDiferenca+' = (select isnull(max(cod_sincronismo_fim),0) from ' +@DB+'.dbo.sincronismo where

    -- tp_processo =''I'' and cod_tp_classificacao =1 and cod_rep_fiscal ='''+@codrf+'''')

    -- if (select count(sessaofim) from FicheirosImportar where

    -- sessaoinicio ='T' and sessaofim > @MaxTotalidade

    -- and sessaofim > @MaxDiferenca and codrf =''+@codrf+'') <> 0

    --

    -- BEGIN

    --

    -- set @MaxTotalidade = (select isnull(max(sessaofim),0) from FicheirosImportar where

    -- sessaoinicio ='T' and sessaofim > @MaxTotalidade

    -- and sessaofim > @MaxDiferenca and codrf =''+@codrf+'')

    --

    --

    -- update FicheirosImportar set valido =1 where codrf = ''+@codrf+''

    -- and sessaoinicio ='T' and sessaofim = @MaxTotalidade

    --

    -- END

    --

    --

    --

    -- if (@MaxTotalidade > @MaxDiferenca)

    -- BEGIN

    --

    -- if (select count(sessaofim) from FicheirosImportar where

    -- sessaoinicio <> 'T' and sessaofim > @MaxTotalidade and codrf =''+@codrf+'') <> 0

    --

    --

    --

    -- update FicheirosImportar set valido =1 where

    -- sessaoinicio <> 'T' and sessaofim > @MaxTotalidade and codrf =''+@codrf+''

    --

    --

    --

    --

    -- END

    -- ELSE

    --

    --

    -- if ('select count(sessaofim) from FicheirosImportar where

    -- sessaoinicio <> ''T'' and sessaofim > @MaxTotalidade and codrf ='''+@codrf+'''

    -- and (sessaofim > '+@MaxDiferenca+' or sessaofim not in

    -- (select cod_sincronismo_fim from '+@db+'.dbo.sincronismo

    -- where cod_rep_fiscal ='''+@codrf+''' and cod_tp_classificacao =1

    -- and cod_sincronismo_fim > '+@MaxTotalidade+'))')<> 0

    --

    --

    -- BEGIN

    --

    --

    -- exec('update FicheirosImportar set valido =1 where

    -- sessaoinicio <> ''T'' and sessaofim > '+@MaxTotalidade+' and codrf ='''+@codrf+'''

    -- and (sessaofim > '+@MaxDiferenca+' or sessaofim not in(

    -- select cod_sincronismo_fim from '+@db+'.dbo.sincronismo

    -- where cod_rep_fiscal ='''+@codrf+''' and cod_tp_classificacao =1

    -- and cod_sincronismo_fim > '+@MaxTotalidade+'))')

    --

    --

    --

    --

    -- END

    --

    --

    --

    --

    -- update FicheirosImportar set ordem =1 where valido =1 and

    -- sessaoinicio ='T' and codrf =''+@codrf+''

    --

    --

    --

    -- update ficheirosimportar set ordem = a.row +1

    -- from

    -- (select ROW_NUMBER() OVER( ORDER BY sessaoinicio) AS Row,codrf RF,

    -- sessaoinicio SI,sessaofim SF

    -- FROM FicheirosImportar

    -- WHERE sessaoinicio <> 'T' and valido =1) a

    -- where ficheirosimportar.codrf = RF and ficheirosimportar.sessaoinicio = SI

    -- and ficheirosimportar.sessaofim =SF and RF = ''+@codrf+''

    --

    --

    --

    FETCH NEXT FROM db_cursor INTO @codrf

    END

    CLOSE db_cursor

    DEALLOCATE db_cursor

    END

    Error:

    Msg 8115, Level 16, State 8, Line 1

    Arithmetic overflow error converting varchar to data type numeric.

  • Ok, I simplified you original code and took out all of the commented stuff so we can see this a bit easier for testing. What I need is some sample data (not real if it is confidential) to debug. Here is the code I have put together, please give me some insert statements for each of the 2 tables so that I can get some data to test with.

    USE test

    GO

    CREATE TABLE [dbo].[FicheirosImportar] (

    [SessaoInicio] [varchar](5) NULL

    ,[SessaoFim] [varchar](5) NULL

    ,[Codrf] [varchar](5) NULL

    ,[Nome] [varchar](500) NULL

    ,[Caminho] [varchar](500) NULL

    ,[Valido] [bit] NULL

    ,[Ordem] [int] NULL

    ) ON [PRIMARY];

    GO

    CREATE TABLE [dbo].[SINCRONISMO] (

    [CODIGO] [bigint] IDENTITY(1, 1) NOT NULL

    ,[DT_INI_PROC] [datetime] NOT NULL

    ,[DT_FIM_PROC] [datetime] NULL

    ,[TP_PROCESSO] [char](1) NOT NULL

    ,[COD_TP_CLASSIFICACAO] [int] NOT NULL

    ,[UTILIZADOR] [int] NOT NULL

    ,[COD_SINCRONISMO] [bigint] NULL

    ,[FICHEIRO] [varchar](255) NOT NULL

    ,[COD_REP_FISCAL] [varchar](5) NULL

    ,[COD_SINCRONISMO_INI] [bigint] NULL

    ,[COD_SINCRONISMO_FIM] [bigint] NULL

    ,[STATUS] [int] NOT NULL

    ,CONSTRAINT [PK_SINCRONISMO] PRIMARY KEY CLUSTERED ([CODIGO] ASC) WITH (

    PAD_INDEX = OFF

    ,STATISTICS_NORECOMPUTE = OFF

    ,IGNORE_DUP_KEY = OFF

    ,ALLOW_ROW_LOCKS = ON

    ,ALLOW_PAGE_LOCKS = ON

    ) ON [PRIMARY]

    ) ON [PRIMARY];

    GO

    CREATE PROCEDURE [dbo].[spSGCT_VALIDA_SGCTLocais] (@DB AS VARCHAR(50))

    --ALTER PROCEDURE [dbo].[spSGCT_VALIDA_SGCTLocais] (@DB as varchar(50))

    AS

    BEGIN

    SET NOCOUNT ON

    DECLARE @MaxTotalidade AS VARCHAR(100)

    ,@MaxDiferenca AS BIGINT

    ,@Inicio AS VARCHAR(10)

    ,@Fim AS VARCHAR(10)

    ,@CODRF AS VARCHAR(10)

    ,@SQLString NVARCHAR(500)

    ,@ParmDefinition NVARCHAR(500)

    ,@IntVariable INT

    DECLARE db_cursor CURSOR

    FOR

    SELECT DISTINCT (codrf)

    FROM FicheirosImportar

    OPEN db_cursor

    FETCH NEXT

    FROM db_cursor

    INTO @codrf

    DELETE

    FROM FicheirosImportar

    WHERE sessaoinicio <> sessaofim

    AND sessaoinicio <> 'T'

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @SQLString = 'select @maxVal = isnull(max(cod_sincronismo_fim),0)

    from ' + @DB + '.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =2

    and cod_rep_fiscal =' + @codrf + ''

    SET @ParmDefinition = '@maxVal varchar(100) OUTPUT'

    EXECUTE sp_executesql @SQLString

    ,@ParmDefinition

    ,@maxVal = @MaxTotalidade OUTPUT

    SELECT @MaxTotalidade

    FETCH NEXT

    FROM db_cursor

    INTO @codrf

    END

    CLOSE db_cursor

    DEALLOCATE db_cursor

    END

    GO

    Jared
    CE - Microsoft

  • Data for table sincronismo:

    18458 2012-03-06 17:35:06.000 2012-03-06 18:07:51.000 I 1 38 NULL PastaExportacao SGCTlocal - 4.20 - 20120306 162529 - D - 56.rar 4.20 56 56 0

    18459 2012-03-06 17:35:06.000 2012-03-06 18:20:28.000 I 1 38 57 PastaExportacao SGCTlocal - 4.20 - 20120228 163220 - D - 51.rar 4.20 61 61 1

    18460 2012-03-06 18:21:07.000 2012-03-06 18:32:04.000 E 2 38 851 D:\Documents and Settings\Crispim.Costa.IMPOSTOS\Ambiente de trabalho\06-03-2012\PastaExportacao SGCTCentral - 20120306 183202.rar NULL NULL NULL 0

    18461 2012-03-06 10:08:04.250 2012-03-06 10:08:04.250 E 1 1 851 FALSO NULL NULL NULL 0

    18462 2012-05-02 14:21:02.000 2012-05-02 14:35:56.000 E 1 1 852 D:\DBs\PastaExportacao SGCTCentral - 20120502 143553.rar NULL NULL NULL 0

    18463 2012-05-02 15:55:17.000 2012-05-02 16:14:53.000 E 2 1 853 E:\PastaExportacao SGCTCentral - 20120502 161450.rar NULL NULL NULL 0

    18464 2012-06-14 10:43:54.000 2012-06-14 10:43:54.000 I 1 1 1861 PastaExportacao SGCTlocal - 4.01 - 20120508 155638 - D - 1215.rar 4.01 6300 6300 1

    18465 2012-06-14 10:46:15.000 2012-06-14 10:48:50.000 I 1 1 NULL PastaExportacao SGCTlocal - 4.01 - 20120508 155638 - D - 1215.rar 4.01 1861 1861 0

  • Data for table ImportarFicheiros:

    T 1118 4.01 PastaExportacao SGCTlocal - 4.01 - 20120523 112426 - T - 1118_CS.rar D:\DBs\exp\teste\PastaExportacao SGCTlocal - 4.01 - 20120523 112426 - T - 1118_CS.rar_Dir\ 0 NULL

    1119 1119 4.01 PastaExportacao SGCTlocal - 4.01 - 20120523 112924 - D - 1119_CS.rar D:\DBs\exp\teste\PastaExportacao SGCTlocal - 4.01 - 20120523 112924 - D - 1119_CS.rar_Dir\ 0 NULL

  • It was the '' in the codrf.

    The correct is:

    SET @SQLString = 'select @maxVal = isnull(max(cod_sincronismo_fim),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =2

    and cod_rep_fiscal ='''+@codrf+''''

    Thanks

Viewing 10 posts - 16 through 24 (of 24 total)

You must be logged in to reply to this topic. Login to reply