skcratch Posted May 11, 2008 at 08:56 PM Report Share #184585 Posted May 11, 2008 at 08:56 PM Viva! Estou a tentar criar uma stored procedure simples que verifique se um nome, que é passado como parâmetro, existe na tabela Pessoa. USE [Aula 6] GO CREATE PROC spDeleteActor @Nome NVARCHAR(200) AS IF((SELECT IdPessoa FROM Pessoa WHERE Nome = @Nome) = NULL) BEGIN print 'A pessoa nao existe' END ELSE BEGIN print 'A pessoa existe' END GO EXEC spDeleteActor 'Rui' As minhas dúvidas residem na instrução IF, porque não sei qual é o valor de retorno da instrução SELECT caso a pessoa que procuro não exista. De notar o nome (Rui) que é passado na chamada da stored procedure não existe na base de dados e que é sempre imprimido 'A pessoa existe'. Grato desde já por qualquer ajuda, Cumps! 😄 Link to comment Share on other sites More sharing options...
DanielAmorim Posted May 11, 2008 at 11:43 PM Report Share #184624 Posted May 11, 2008 at 11:43 PM Boas, Para resolveres este tipo de questões o mais fácil é usares as funções EXISTS / NOT EXISTS. Estas funções retornam um boleano, true caso exista algum tuplo e false caso não exista. Para o teu SP ficaria assim: IF(NOT EXISTS(SELECT IdPessoa FROM Pessoa WHERE Nome = @Nome)) Daniel Amorim VP for xRTML http://www.xrtml.org http://www.realtime.co Link to comment Share on other sites More sharing options...
M6 Posted May 17, 2008 at 09:23 PM Report Share #185726 Posted May 17, 2008 at 09:23 PM Uma técnica comum é fazer um count, se retornar zero, então não existe. 😉 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
hardcore Posted May 18, 2008 at 09:53 PM Report Share #185964 Posted May 18, 2008 at 09:53 PM sim, concordo que essa seja a forma mais facil Link to comment Share on other sites More sharing options...
DanielAmorim Posted May 18, 2008 at 10:37 PM Report Share #185978 Posted May 18, 2008 at 10:37 PM Uma técnica comum é fazer um count, se retornar zero, então não existe. 🙂 Um count para verificar se existe algo? Se existe um função própria para isso porque complicar? 😉 Daniel Amorim VP for xRTML http://www.xrtml.org http://www.realtime.co Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now