Inquiridor
Select + Delete

Pergunta
-
Olá,
Preciso fazer uma exclusão dependendo condição passada pelo parâmetro. Vejam a query.
if
not exists ( select idCheque from tbChequeDevolvido where idCheque = @idCheque ) delete from tbCheque from tbCheque inner join tbBanco on tbCheque.idBanco = tbBanco.idBanco inner join tbEmitenteCheque on tbCheque.idEmitente = tbEmitenteCheque.idEmitente where (tbCheque.Cheque = upper(@Cheque)) and (tbEmitenteCheque.Nome = upper(@Emitente)) and (tbBanco.Banco = upper(@Banco))O cheque só pode ser excluido da tabela tbCheque se ele não existir na tabela tbChequeDevolvido porém, quando executo o SQL acima, a exclusão na tabela tbCheque está ocorrendo.
Alguma dica ?????
Obrigado.
Todas as Respostas
-
Olá Gilberto,
Podemos verificar o que aconteceu, mas acho que esse IF é desnecessário. Você poderia combinar tudo em uma única query. Ex:
Code Snippetdelete
from tbCheque from tbCheque inner join tbBanco on tbCheque.idBanco = tbBanco.idBanco inner join tbEmitenteCheque on tbCheque.idEmitente = tbEmitenteCheque.idEmitente where (tbCheque.Cheque = upper(@Cheque)) and (tbEmitenteCheque.Nome = upper(@Emitente)) and (tbBanco.Banco = upper(@Banco))and
tbCheque
not exists (select idcheque from tbChequeDevolvido.IdCheque = tbCheque.IdCheque)[ ]s,
Gustavo
-
Então gustavo,
Esta query, está numa stored procedure no banco fiz a mudança conforme sua sugestão e apresentou o seguinte erro:
Msg 156, Level 15, State 1, Procedure pCheque, Line 93
Incorrect syntax near the keyword 'exists'.
Msg 102, Level 15, State 1, Procedure pCheque, Line 94
Incorrect syntax near '='.
Obrigado.
-
Olá Gilberto,
A instrucao esta aparentemente correta, tente colocar um begin end para separar e faca um teste printando algum valor. assim voce pode assegurar que realmente ele vai executar um bloco específico caso exista dados no seelct acima, ok.
Code Snippetif not exists
( select idCheque from tbChequeDevolvido where idCheque = @idCheque )begin
delete from tbCheque from tbCheque inner join tbBanco on tbCheque.idBanco = tbBanco.idBanco inner join tbEmitenteCheque on tbCheque.idEmitente = tbEmitenteCheque.idEmitente where (tbCheque.Cheque = upper(@Cheque)) and (tbEmitenteCheque.Nome = upper(@Emitente)) and (tbBanco.Banco = upper(@Banco))end
else
begin
print 'Existe um cheque devolvido!'
end
-