Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert Trigger not working properly
    primarykey
    data
    text
    <p>I have created the following trigger for insert in the deleted user table the corresponding record has to be deleted from the userlogin table.</p> <p>The trigger involves the following statements:</p> <pre><code>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 </code></pre> <p>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.</p> <p>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.</p> <p>For your reference I am including the table structure for both the deleteduser and userlogin is as follows:</p> <pre><code>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) </code></pre> <p>Please help me with the changes I should make to insert the row in the deleteduser table and remove (delete) it from userlogin table.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload