Jump to content
Fórum Script Brasil
  • 0

Cursor no SQL


Tonblz

Question

Boa dia galera,

podem em ajudar, ao desenvolver uma proc no sql 2008,usando cursor, da os erros abaixo

Msg 16916, Level 16, State 1, Procedure dbo.SPI_WELLINGTON, Line 135

A cursor with the name 'C_PARADAS' does not exist.

Msg 16916, Level 16, State 1, Procedure dbo.SPI_WELLINGTON, Line 136

A cursor with the name 'C_PARADAS' does not exist.

obs: mas eu fiz a criação do cursos.

abaixo o código da proc

USE [sPI_BD_PER]

GO

/****** Object: StoredProcedure [dbo].[dbo.SPI_WELLINGTON] Script Date: 12/06/2011 19:49:56 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[dbo.SPI_WELLINGTON]

AS

DECLARE @DTINI AS DATETIME,@DTFIM AS DATETIME

BEGIN

DECLARE @ERRO AS VARCHAR(500)

BEGIN TRY

DECLARE @IDPARADA AS INT,@IDMAQUINA AS INT,@IDORDEM AS INT,@IDMOTIVO1 AS INT

DECLARE @IDMOTIVO2 AS INT,@IDMOTIVO3 AS INT

DECLARE @T1 AS FLOAT,@T2 AS FLOAT,@T3 AS FLOAT

DECLARE @HORAINI AS DATETIME, @HORAFIM AS DATETIME

DECLARE @DATAI AS DATE,@DATAF AS DATE

--PARAMETROS FIXOS

DECLARE @HORA00 AS DATETIME,@HORA06 AS DATETIME,@HORA14 AS DATETIME,@HORA22 AS DATETIME

DECLARE @HORA0559 AS DATETIME,@HORA1359 AS DATETIME,@HORA2159 AS DATETIME, @HORA2359 AS DATETIME

SET @HORA00 ='00:00:00'

SET @HORA06 ='06:00:00'

SET @HORA14 ='14:00:00'

SET @HORA22 ='22:00:00'

SET @HORA0559 ='05:59:59'

SET @HORA1359 ='13:59:59'

SET @HORA2159 ='21:59:59'

SET @HORA2359 ='23:59:59'

--PARAMETROS VARIAVEIS

DECLARE @DATAINI AS DATETIME, @DATAFIM AS DATETIME

SET @DTINI = '20110920 00:00:00'

SET @DTFIM = '20110923 23:59:59'

-- LIMPA TABELA TEMP PARADAS POR TURNO

DELETE FROM SPI_TB_MAQUINA_PARADAS_TEMP

-- CURSOR QUE BUSCAR AS PARADAS DURANTE PERÍODO SOLICITADO

DECLARE C_PARADAS CURSOR FOR

SELECT PAR.ID_MAQUINA_PARADAS

,PAR.ID_MAQUINA

,PAR.ID_ORDEM_PRODUCAO

,PAR.ID_MOTIVO1

,PAR.ID_MOTIVO2

,PAR.ID_MOTIVO3

,CONVERT(VARCHAR(19),DT_INICIO,103) AS DT_INI

,CONVERT(VARCHAR(19),DT_FIM,103) AS DT_FIN

,CONVERT(VARCHAR(19),DT_INICIO,108) AS HRA_INI

,CONVERT(VARCHAR(19),DT_FIM,108) AS HR_FIN

FROM SPI_TB_MAQUINA_PARADAS AS PAR

INNER JOIN SPI_TB_MAQUINA AS MAQ ON MAQ.ID_MAQUINA = PAR.ID_MAQUINA

WHERE PAR.ID_MAQUINA_PARADAS= 450252

-- ABRIR CURSOR

OPEN C_PARADAS

FETCH NEXT FROM C_PARADAS INTO @IDPARADA,@IDMAQUINA,@IDORDEM,@IDMOTIVO1

,@IDMOTIVO2,@IDMOTIVO3,@DATAINI,@DATAFIM,@HORAINI,@HORAFIM

WHILE (@@FETCH_STATUS = 0) BEGIN

SET @IDPARADA = @IDPARADA

SET @IDMAQUINA = @IDMAQUINA

SET @IDORDEM = @IDORDEM

SET @IDMOTIVO1 = @IDMOTIVO1

SET @IDMOTIVO2 = @IDMOTIVO2

SET @IDMOTIVO3 = @IDMOTIVO3

SET @IDMOTIVO2 = @IDMOTIVO2

SET @T1 = 0

SET @T2 = 0

SET @T3 = 0

IF ((@DATAINI >= @DTINI AND @DATAFIM <= @DTFIM)) BEGIN

--COMEÇA E TERMINA NO PERÍODO

IF (DATEDIFF(DAY,@DATAINI,@DATAFIM)=0)BEGIN

SET @DATAI = @DATAINI

SET @DATAF = @DATAFIM

IF (@HORAFIM< @HORA06) BEGIN

SET @T3 = DATEDIFF(MINUTE,@HORAINI,@HORAFIM) END ELSE IF (@HORAINI < @HORA06) AND (@HORAFIM<= @HORA1359) BEGIN

SET @T1 = DATEDIFF(MINUTE,@HORA06,@HORAFIM)

SET @T3 = DATEDIFF(MINUTE,@HORAINI,@HORA06)

END ELSE IF (@HORAINI < @HORA06) AND (@HORAFIM<= @HORA2159) BEGIN

SET @T1 = DATEDIFF(MINUTE,@HORA06,@HORA14)

SET @T2 = DATEDIFF(MINUTE,@HORA14,@HORAFIM)

SET @T3 = DATEDIFF(MINUTE,@HORAINI,@HORA06) END

END

END

--INSERINDO DADOS NA TABELA (SPI_TB_MAQUINA_PARADA_TURNOS_TEMP)

INSERT INTO SPI_TB_MAQUINA_PARADAS_TEMP ( IDPARADA,IDMAQUINA,IDORDEM,IDMOTIVO1,IDMOTIVO2, IDMOTIVO3,DATAINICIAL,DATAFINAL,TURNO1,TURNO2,TURNO3)

VALUES ( @IDPARADA,@IDMAQUINA,@IDORDEM,@IDMOTIVO1,@IDMOTIVO2,@IDMOTIVO3,@DATAI,@DATAF,@T1,@T2,@T3)

--PASSANDO PARA PROXIMO REGISTRO

FETCH NEXT FROM C_ORDEM_MAQ INTO @IDPARADA,@IDMAQUINA,@IDORDEM,@IDMOTIVO1,@IDMOTIVO2

,@IDMOTIVO3,@DATAINI,@DATAFIM,@HORAINI,@HORAFIM

END--WHILE

CLOSE C_PARADAS

DEALLOCATE C_PARADAS

END TRY

BEGIN CATCH

SET @ERRO = ERROR_PROCEDURE() + ': Line(' + CONVERT(varchar,ERROR_LINE()) + ') # ' + ERROR_MESSAGE()

INSERT INTO SPI_TB_LOG_PROCEDURES (DATA, MENSAGEM) VALUES (GETDATE(), @ERRO)

CLOSE C_PARADAS

DEALLOCATE C_PARADAS

CLOSE C_PARADAS

DEALLOCATE C_PARADAS

END CATCH

END

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Bom dia Tonblz,

Quando você vai apontar o cursor para a próxima posição, não está referenciando o cursor em aberto:

Está assim:

--PASSANDO PARA PROXIMO REGISTRO
FETCH NEXT FROM C_ORDEM_MAQ INTO @IDPARADA,@IDMAQUINA,@IDORDEM,@IDMOTIVO1,@IDMOTIVO2
,@IDMOTIVO3,@DATAINI,@DATAFIM,@HORAINI,@HORAFIM
Seria assim:
--PASSANDO PARA PROXIMO REGISTRO
FETCH NEXT FROM C_PARADAS INTO @IDPARADA,@IDMAQUINA,@IDORDEM,@IDMOTIVO1,@IDMOTIVO2
,@IDMOTIVO3,@DATAINI,@DATAFIM,@HORAINI,@HORAFIM

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...