Solved

Inserting a column in a table that creates an ID and row number

Posted on 2016-10-19
4
69 Views
Last Modified: 2016-10-20
I have a trigger on a table (like an audit trigger) that updates a backup table whenever the main table is updated. If someone updates the main table "Client", all the changes are captured along with the old records in the "ClientBackup" table.
I created the backup table using INSERT INTO

What I want to do is add a column to this backup table for identification purposes, possibly to create a unique key (but not certain I need that yet), but I do want to add the column with row numbers to the backup table and auto increment that when new data is added.

Because this is updated via an UPDATE Trigger the column would need to auto update whenever the trigger fires.

I know I can add a column to an existing table in order to number the rows with

ALTER TABLE CLIENT ADD id INT IDENTITY(1,1)

The current table has 471 rows if I run the above code and add the IDENTITY column I'll have rows 1 through 471. How would I create a column that auto updates the next time the trigger fires? I'm learning SQL but in other code I would create a variable that holds the value of IDENTITY and then add one to the current value, but can I do that inside a trigger and in SQL?

Here is the trigger code
---- CODE----

CREATE TRIGGER [cd].[trg_MOTS_CLIENT_UPDATE]
ON [cd].[client471]
AFTER UPDATE
AS
SET NOCOUNT ON;
BEGIN
INSERT INTO cd.client471bckup
    ([uniqueid_c], [clientid_c],
    [txstatus_c], [compemploy01_c], [compemploy02_c], [compemploy03_c],
      [compemploy04_c], [compemploy05_c], [compemploy06_c], [compemploy07_c], [compemploy08_c],
    [compemploy09_c], [compemploy10_c], [compemploy11_c], [compemploy12_c],
    [livingarr_c], [livingarrchange_d], [livingarrlast_c], [lastsubstat_d], [createuser_c], [create_dt]
     )
SELECT
    i.uniqueid_c, i.clientid_c, i.txstatus_c,i.compemploy01_c,i.compemploy02_c,i.compemploy03_c,i.compemploy04_c,
    i.compemploy05_c,i.compemploy06_c,i.compemploy07_c,i.compemploy08_c,
    i.compemploy09_c,i.compemploy10_c, i.compemploy11_c, i.compemploy12_c,
    i.livingarr_c,i.livingarrchange_d,i.livingarrlast_c,i.lastsubstat_d,i.createuser_c, i.create_dt
FROM cd.client471 c
Inner join inserted i on c.clientid_c = i.clientid_c

--END---
0
Comment
Question by:John Mahoney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 41850865
You don't need to worry about that column.  EG: you add the identity column and then you do your insert as normal (ignoring the identity column).  When you do the next select it will auto increment for you.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 41852478
For a column with the IDENTITY property, SQL Server automatically generates the value for the column during an INSERT.  Therefore, you can't provide the value yourself (exception: you can explicitly "tell" SQL that you will be providing the identity values to be INSERTed).

For example:
CREATE TABLE table1 (column1 int IDENTITY(1, 1) NOT NULL, column2 varchar(30))
INSERT INTO table1 VALUES('column2_value')
Note that you basically ignore the identity column on a normal INSERT statement.
0
 

Author Comment

by:John Mahoney
ID: 41852484
Thank you.
0
 

Author Closing Comment

by:John Mahoney
ID: 41852488
Again thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question