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 2005 Forums
 Transact-SQL (2005)
 Insert data from one table to another

Author  Topic 

nithin.gujjar
Starting Member

19 Posts

Posted - 2008-09-25 : 11:08:56
I have 2 tables:
1."Employee" which has 20 odd fields.
2."User" has 80 odd fields.
When a record is inserted in Employee table a trigger in this table will insert a record in User Table with 20 odd values that Employee has,plus the other 60 odd values to the remaining columns in User table based on a value in a column in Employee Table which can take 5 different values.

The values for 60 odd fields in user needs to be hard coded in the trigger.

My question is simple, which is the best way to do this? So as to minimize coding and so that the execution of this trigger does not slow down the system.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-25 : 11:12:30
Just do like this
INSERT INTO Users
SELECT empvalue1,empvalue2,...empvalue20,
CASE WHEN empfield= somevalue then constant1
when empfield= somevalue2 then constan2
...
END AS HradCoded1,
CASE WHEN empfield= somevalue then constant1
when empfield= somevalue2 then constan2
...
END AS HradCoded2
...
CASE WHEN empfield= somevalue then constant1
when empfield= somevalue2 then constan2
...
END AS HradCoded60
Go to Top of Page
   

- Advertisement -