Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Lab Design

Qual o erro dessa SP?

Recommended Posts

USE [ridgid]

GO

/****** Object: StoredProcedure [dbo].[OS_finaliza] Script Date: 11/23/2007 18:14:06 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[OS_finaliza]

@osId int,

@RET int output

AS

DECLARE @tipo int

DECLARE @assId int

DECLARE @saldoAnterior decimal(12,2)

DECLARE @saldoAtual decimal(12,2)

DECLARE @maoDeObra decimal(12,2)

DECLARE @historico nVarchar(100)

SELECT @tipo=tipo from ordemServico WHERE id=@osId

if @tipo=1

begin

begin transaction

PRINT 'osId=' + str(@osId)

DECLARE curOS CURSOR FOR

SELECT numCat, quantidade FROM ordemServico_itens WHERE osId=@osId

OPEN curOS

PRINT 'The cursor contains ' + CONVERT(char(3), @@CURSOR_ROWS) + ' rows'

DECLARE @quantidade int

DECLARE @numCat int

 

FETCH NEXT FROM curOS INTO @quantidade, @numCat

print 'fetch_status='+str(@@FETCH_STATUS)

WHILE @@FETCH_STATUS = 0

BEGIN

UPDATE assEstoque

SET estoque=estoque-@quantidade

WHERE assId=@assId

AND numCat=@numCat

 

FETCH NEXT FROM curOS INTO @quantidade, @numCat

END

CLOSE curOS

DEALLOCATE curOS

end

else

begin

SELECT @saldoAnterior=ca.saldo, @maoDeObra=os.maoDeObra, @assID=os.assId

FROM distribuidores ca, ordemServico os

WHERE ca.did=os.assId AND os.id=@osId

SET @saldoAtual=@saldoAnterior + @maoDeObra

SET @historico='Reebolso de Garantia OS nr.'+rtrim(str(@osId))

begin transaction

INSERT INTO assContas(assId, osId, historico, saldoAnterior, lancto, saldoAtual, data)

VALUES(@assId,@osId,@historico,@saldoAnterior,@maoDeObra,@saldoAtual,getdate())

UPDATE distribuidores

SET saldo=@saldoAtual WHERE did=@assId

end

 

if @@error <> 0

begin

rollback transaction

return(1)

end

else

begin

commit transaction

return(0)

end

Eu executo ela pelo SQL server management e retorna

osId= 257

The cursor contains -1 rows

fetch_status= 0

 

(0 row(s) affected)

 

(0 row(s) affected)

 

(1 row(s) affected)

 

(1 row(s) affected)

 

ele num ta achando nada na consulta agora se faço direto:

 

 

SELECT numCat, quantidade

FROM ordemServico_itens

WHERE (osId = 257)

retorna:

45327 3

59827 3

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eu mudei um pouco a SP

DECLARE @sql nVarchar(100)

begin transaction

-- PRINT 'osId=' + str(@osId)

SET @sql='SELECT numCat, quantidade FROM ordemServico_itens WHERE osId='+str(@osId)

-- PRINT 'sql=' + @sql

SET NOCOUNT ON

DECLARE curOS CURSOR FOR

SELECT @sql

OPEN curOS

PRINT 'The cursor contains ' + CONVERT(char(3), @@CURSOR_ROWS) + ' rows'

DECLARE @quantidade int

DECLARE @numCat int

FETCH NEXT FROM curOS INTO @quantidade, @numCat

-- print 'fetch_status='+str(@@FETCH_STATUS)

WHILE @@FETCH_STATUS = 0

BEGIN

UPDATE assEstoque

SET estoque=estoque-@quantidade

WHERE assId=@assId

AND numCat=@numCat

 

FETCH NEXT FROM curOS INTO @quantidade, @numCat

END

CLOSE curOS

DEALLOCATE curOS

Pelo menos agora a consulta ta retornando alguma coisa

 

The cursor contains 1 rows

 

Msg 16924, Level 16, State 1, Procedure OS_finaliza, Line 26

Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.

 

Mas ta dando um erro estranho.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ola

 

faz o seguinte

 

coloque estas duas linhas

 

DECLARE @quantidade int

DECLARE @numCat int

 

depois do

 

begin transaction

 

Procure no books on line "declare cursor"

 

abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.