Skip to content
Advertisement

Didnt print on Procedure on SQL

So I try to print a message when I have any confirmation or error. I have this table [Friendship] that have the ID, friend mail, their mail and the date that have been confirm the friendship,

Table without data

1

Create PROCEDURE dbo.pFriendship @Usermail varchar(50), @Friendmail varchar(50) As
    if((Select COUNT(1) From Friendship) < 50)
    begin
        if(@Usermail = @Friendmail)
        begin
            print 'You can not be your friend.';
        end
        else
        begin
        begin TRAN
            Insert Into Friendship (Date, Mail, Friend_Mail) VALUES (CONVERT(DATE, GETDATE()), @Usermail, @Friendmail)
            if(@@ERROR <> 0)
            begin
                print 'Fail!';
            end
            print 'User ' + @Usermail+ ' is now friend of ' + @Friendmail;
            commit TRAN;
        end
    end
    else
    begin
            print 'You can not have more friends.';
    end

When I use the EXEC for this PROCEDURE, I have the insert values, but not the print´s acctions… Also, If I try to use the same mails, also insert the values. Didnt check the condition

EXEC dbo.pFriendship 'mailTest@hotmail.com', 'mailTest2@gmail.com'

Confirm insert without any use of Print of my PROCEDURE

2

What I was hoping for is like

(1 row affected)
User mailtest@gmail.com is now friend of mailtest2@gmail.com
...

Or the error at @@ERROR or the equals mails error (if applied)

Advertisement

Answer

Just remove the last else

Create PROCEDURE dbo.pFriendship @Usermail varchar(50), @Friendmail varchar(50) As
if((Select COUNT(1) From Friendship) < 50)
begin
    if(@Usermail = @Friendmail)
    begin
        print 'You can not be your friend.';
    end
    else
    begin
    begin TRAN
        Insert Into Friendship (Date, Mail, Friend_Mail) VALUES (CONVERT(DATE, GETDATE()), @Usermail, @Friendmail)
        if(@@ERROR <> 0)
        begin
            print 'Fail!';
        end
        print 'User ' + @Usermail+ ' is now friend of ' + @Friendmail;
        commit TRAN;
    end
end

begin
        print 'You can not have more friends.';
end
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement