| Author |
Topic |
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-06 : 14:53:34
|
| can someone help me?Write a stored procedure for Microsoft SQL Server that will accomplish the following:We have a script that runs on the mail server and processes emails that are bounced backto us (incorrect email address, account inactive, etc.). The script will return the emailaddress and the reason the email failed (SMTP code). We then need to use the emailaddress and flag the corresponding account from a database table named 'accounts'. Writea stored procedure that takes as input the following: 1) email address, and 2) SMTP failurecode. This stored procedure should then return the following result codes: 1 (account wasremoved from DB successfully), or 0 (an error occurred and the account was not removed).Here are the DB specifics:DB Name: online_dbAffected tables:Table Fieldsaccounts id, name, email, status, smtp_codeaccount_status id, code (where id = 1/code = active),2/unsubscribed, 3/flagged) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-06 : 15:56:31
|
| I think I need use "using Return"http://www.sqlteam.com/article/stored-procedures-returning-databut sincerely I did not understand exactly what they are looking for.the first part it should be:CREATE PROCEDURE dbo.spReturn ( @email_address nvarchar(50), @SMTP_failure_code nvarchar(MAX) )ASbut then?I I understand wellI should retrieve 1 or 0 thenwrite status = 3 (flagged)that's it? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-07 : 07:52:52
|
| do you think this could be the solution?ALTER PROCEDURE dbo.sp_remEmail ( @email_address nvarchar(50), @SMTP_failure_code nvarchar(50) ) AS SET @email_address = (SELECT email FROM accounts WHERE (email = @email_address)) IF (@email_address is NULL) AND (@SMTP_failure_code is NULL) RETURN 0 ELSE RETURN 1if not, can you explain me yours? |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-05-07 : 08:19:35
|
quote: Originally posted by Alessandro SET @email_address = (SELECT email FROM accounts WHERE (email = @email_address))
I don't think this is what you want to do. This will do one of three things. If @email_address does not exist in the account table, it will SET @email_address = NULL. If @email_address exists only once in the account table, it will do nothing. (It will actually SET @email_address equal to itself.)If @email_address exists more than once in the account table, it will generate an error.If you simple want to return a 0 when @SMTP_failure_code IS NULL AND when @email_address exists in the Accounts table, otherwise return 1, then do this instead:ALTER PROCEDURE dbo.sp_remEmail@email_address nvarchar(50),@SMTP_failure_code nvarchar(50)AS IF @SMTP_failure_code is NULL AND EXISTS ( SELECT email FROM accounts WHERE email = @email_address) RETURN 0 ELSE RETURN 1 You should also consider the possibility that @SMTP_failure_code may be 0 if there is no failure, rather than NULL.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-07 : 08:20:22
|
| maybe that's is better ...do you think this could be the solution?if not, can you explain me yours?ALTER PROCEDURE dbo.sp_remEmail ( @email_address nvarchar(50), @SMTP_failure_code nvarchar(50) ) AS SET @email_address = (SELECT email FROM accounts WHERE (email = @email_address)) IF ((@email_address is NULL) OR (@email_address = '') OR (@email_address = 0)) AND ((@SMTP_failure_code is NULL) OR (@SMTP_failure_code = '') OR (@SMTP_failure_code = 0)) RETURN 0 ELSE RETURN 1 |
 |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-07 : 09:27:21
|
| thank youso at the end the solution it could be this one?ALTER PROCEDURE dbo.sp_remEmail@email_address nvarchar(50),@SMTP_failure_code nvarchar(50)AS IF (@SMTP_failure_code is NULL) or (@SMTP_failure_code = 0) or (@email_address = 0) AND EXISTS ( SELECT email FROM accounts WHERE email = @email_address) RETURN 0 ELSE UPDATE accounts SET status=3, smtp_code=@SMTP_failure_code WHERE email=@email_address RETURN 1 |
 |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-07 : 09:46:51
|
excuse me, pardonALTER PROCEDURE dbo.sp_remEmail@email_address nvarchar(50),@SMTP_failure_code nvarchar(50)AS IF (@SMTP_failure_code is NULL) or (@SMTP_failure_code = 0) AND EXISTS ( SELECT email FROM accounts WHERE email = @email_address) RETURN 0 ELSE UPDATE accounts SET status=3, smtp_code=@SMTP_failure_code WHERE email=@email_address RETURN 1 but I have only a problem,when I put string in @SMTP_failure_code nvarchar(50)the error isRunning [dbo].[sp_remEmail] ( @email_address = email@email.com, @SMTP_failure_code = gvdfl rgkfglf ).Conversion failed when converting the nvarchar value 'gvdfl rgkfglf' to data type int.Conversion failed when converting the nvarchar value 'gvdfl rgkfglf' to data type int.No rows affected.(0 row(s) returned)@RETURN_VALUE = Finished running [dbo].[sp_remEmail]. and my row smtp_code in accounts is nvarchar(50) |
 |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-07 : 12:48:45
|
| any help? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Alessandro
Starting Member
14 Posts |
Posted - 2010-05-07 : 13:37:49
|
| thank you so much |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|