Hi all I have created the following trigger for insert in the deleted user table the corresponding record has to be deleted from the userlogin table.
The trigger involves the following statements:
CREATE TRIGGER trgInsert_ToDeleteFromUserLogin on DELETEDUSER
FOR INSERT
AS
BEGIN
DECLARE
@UserName nvarchar(50),
@UserType nvarchar(30),
@Reason nvarchar(max)
SELECT @UserName = USERNAME, @UserType = UserType, @Reason = Reason FROM INSERTED
INSERT INTO DELETEDUSER (USERNAME,USERTYPE,REASON) VALUES(@USERNAME,@USERTYPE,@REASON)
DELETE FROM USERLOGIN WHERE USERNAME = @UserName
end
But when I insert a record in the deleteduser table the corresponding record is deleted from the userlogin table but it is not inserted to the deleteduser table.
The above trigger does not generate any error, however when I try to insert a record in the Deleted User Table I notice that the record is deleted from the UserLogin table but the same is not been inserted in the DeletedUser table.
For your reference I am including the table structure for both the deleteduser and userlogin is as follows:
Deleted User
CREATE TABLE DELETEDUSER
(
DeletedUserID int identity(1,1),
UserName nvarchar(max),
UserType nvarchar(30),
Reason nvarchar(max)
)
--ALTERING DELETEDUSER TABLE TO SPECIFY THE SIZE OF USERNAME COLUMN FOR IMPOSING FOREIGN KEY CONSTRAINT
ALTER TABLE DELETEDUSER ALTER COLUMN UserName nvarchar(50)
--ALTERING DELETEDUSER TABLE TO ADD FOREIGN KEY CONSTRAINT
ALTER TABLE DELETEDUSER ADD CONSTRAINT fk_UserName Foreign key (UserName) references UserLogin(UserName) on delete cascade
USERLOGIN TABLE:
CREATE TABLE USERLOGIN
(
UserID int identity(1,1) not null,
UserName nvarchar(50) not null,
Password nvarchar(50) not null
)
--ALTER TABLE USERREGISTRATION TO ADD PRIMARYKEY
ALTER TABLE USERLOGIN ADD CONSTRAINT pk_UserName primary key(UserName)
Please help me with the changes I should make to insert the row in the deleteduser table and remove (delete) it from userlogin table.
Thanks in advance!