You need to wrap everything after your ELSE
in a BEGIN
and END
to keep it all as conditional to the IF
.
@user1 uniqueidentifier ,
@user2 uniqueidentifier ,
@mesaj_text varchar(500),
@mesaj_read int
AS
BEGIN
declare @chat_id int
IF EXISTS (SELECT chat_id FROM chat where user1=@user1 and user2 =@user2 or user1=@user2 and user2 =@user1)
BEGIN
SELECT @chat_id =chat_id FROM chat where user1=@user1 and user2 =@user2 or user1=@user2 and user2 =@user1
INSERT INTO mesaj values (@chat_id,@user1,@mesaj_text,getdate(),@mesaj_read)
END
ELSE
BEGIN
insert into chat values (@user1,@user2)
SET @chat_id=SCOPE_IDENTITY()
insert into mesaj values (@chat_id,@user1,@mesaj_text,getdate(),@mesaj_read)
END
END