Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:54:28+00:00 2026-05-31T19:54:28+00:00

I have a table Sample and another SampleLog With these structure I want to

  • 0

I have a table Sample and another SampleLog

With these structure I want to write codes to log. You can see my codes after structures of tables

CREATE TABLE [dbo].[Sample](
   [ID] [int] NULL,
   [Name] [varchar](10) NULL
)

CREATE TABLE [dbo].[SampleLog](
   [ID] [int] NULL,
   [Name] [varchar](10) NULL,
   [Date] [datetime] NULL,
   [UserName] [varchar](100) NULL,
   [Type] [char](1) NULL
) 

I have written this code but it doesn’t work for Delete and Update .

CREATE TRIGGER SampleTrigger ON Sample 
AFTER INSERT, UPDATE, DELETE
AS
   DECLARE 
      @ID int ,
      @Name varchar(10),
      @Date datetime,
      @UserName VARCHAR(128) ,
      @Type CHAR(1) ,
      @sql nvarchar(500)

  SELECT         
      @UserName = SYSTEM_USER ,
      @Date = CONVERT(VARCHAR(8), GETDATE(), 112) 
              + ' ' + CONVERT(VARCHAR(12), GETDATE(), 114)

  IF EXISTS (SELECT * FROM inserted)
  BEGIN
     IF EXISTS (SELECT * FROM deleted)
     BEGIN
        SELECT @Type = 'U'
        select @ID = ID from deleted
        select @Name = Name from deleted
   END
   ELSE
   BEGIN
      SELECT @Type = 'I'
      select @ID = ID from inserted
      select @Name = Name from inserted
   END
 END
 ELSE
BEGIN
   SELECT @Type = 'D'
   select @ID = ID from deleted
   select @Name = Name from deleted
END

insert into SampleLog(ID, Name, Date, UserName, Type) 
values(@ID, @Name, @Date, @UserName, @Type)

SQL Server gives me this error

The row values updateed or deleted either do not make the row unique or they alter multiple rows(2 rows)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:54:29+00:00Added an answer on May 31, 2026 at 7:54 pm

    You’ve coded for single row updates and deletes. Think sets!

    CREATE TRIGGER SampleTrigger ON Sample after INSERT, UPDATE, DELETE
    AS
    SET NOCOUNT ON;
    
    insert into SampleLog
        (ID,Name,Date,UserName,Type)
    SELECT
        D.ID, D.NAME, GETDATE(), SYSTEM_USER,
        CASE WHEN I.ID IS NULL THEN 'D' ELSE 'U' END
    FROM
        DELETED D
        LEFT JOIN
        INSERTED I ON D.ID = I.ID
    UNION ALL
    SELECT
        I.ID, I.NAME, GETDATE(), SYSTEM_USER, 'I'
    FROM
        INSERTED I
        LEFT JOIN
        DELETED D ON D.ID = I.ID
    WHERE
        D.ID IS NULL
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Ruby application I have a hash table: c = {:sample => 1,:another
I have a database with a simple flyweight table referenced by another table. Let's
I have a simple map/reduce job that scans one hbase table, and modifies another
I have the table Tb ID | Name | Desc ------------------------- 1 | Sample
Here is the sample data for test table I have [childId] [parentId] [present] 1
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
I have a table which represents a Contract between two rows in another table.
Basically, what I want to do is to get all children in another table
I want to have a database table that keeps data with revision history (like
I have a right join query (joining a table and another query output). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.