Avatar of superblades
superblades

asked on 

Trigger to copy one field to another in the same table

i need to create a trigger so that when one field is populated it copies that field to another one, e.g.

ive tried the below but to no use

CREATE trigger trgcostcentre
on dbo.tblcostcentre
for update
as
if update(fldcostcentre)
update q
set fldcostcentre_id = fldcostcentre
FROM tblcostcentre.fldcostcentre_id q
INNER JOIN INSERTED i
on i.fldcostcentre_id  = q.fldcostcentre_id
Microsoft SQL Server

Avatar of undefined
Last Comment
Scott Pletcher
Avatar of DonKronos
DonKronos
Flag of United States of America image

I'm confused ???

Your trying to set q.fldcostcentre_id to a new value but you are using the current value of that column in the join ???  Doesn't that mean that the join to q.fldcostcentre_id will fail until q.fldcostcentre_id is updated ???

Am I reading this wrong???
Avatar of puranik_p
puranik_p
Flag of India image

Why not make it a formula field?
Avatar of superblades
superblades

ASKER

DonKronos - your right,

thats probably the main cause of it not working - im a novice so im just making them from snippets here and there and trying to 'fudge' them together to work - without success!

How else could i do it?

Puranik p - could you give me an example

Thanks

Superblades
Avatar of puranik_p
puranik_p
Flag of India image

CREATE TABLE TABLE1
(
myfield varchar(32),
copyfield AS myfield
)

This way whatever value is there in myfield will get copied to copyfield.
This will work for inserts and updates both. No need for a trigger.
ASKER CERTIFIED SOLUTION
Avatar of DonKronos
DonKronos
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo