Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 INSERT without column list

Author  Topic 

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2012-04-18 : 13:34:49
I'm trying to do an INSERT trigger on a table, where if a row that has a code of e.g. A gets inserted, another row should get inserted with the exact same information except the code should be B. I'd like to avoid having to use column names like INSERT Table (Code, Column1,Column2) SELECT 'B',Column1, Column2 FROM Inserted because the table structure can change and then the trigger will break. So how can it do INSERT TableMC SELECT *, except the code column value should be different?


CREATE TRIGGER trgCodeMC ON dbo.TableMC FOR INSERT
AS
IF EXISTS(SELECT * FROM Inserted WHERE Code = 'MC')
BEGIN
--INSERT statement here
END

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-18 : 13:52:08
You have to list out the columns to do this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-04-19 : 04:33:50
Insert the same information to another table and update that column to 'B' if you want to avoid listing columns

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -