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
 General SQL Server Forums
 New to SQL Server Programming
 Trigger help...I think.

Author  Topic 

gjohll
Starting Member

3 Posts

Posted - 2010-09-24 : 14:56:02
This is going to sound odd and I don't want to take the space to explain why, but this is what I need.

Upon inserting a new row in a table, I need to have the value in one column copied into another column in the same table.

I'm assuming this would be done via a trigger.

CREATE TRIGGER trg_name ON tblName
AFTER INSERT
AS

???

How do I get the value in column1 of the newly inserted row and copy it into column2?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-24 : 15:03:10
You should use a computed column instead or just repeat the column in the actual INSERT. Are you using a stored procedure or is it inline sql?

For a trigger, you would have to use an instead of trigger.

INSERT INTO YourTable
SELECT Column1, Column2, Column1
FROM inserted

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

Subscribe to my blog
Go to Top of Page

gjohll
Starting Member

3 Posts

Posted - 2010-09-24 : 15:08:09
The problem is I do not have access to the INSERT statement. The piece of software I'm using is abstracting some of the process from my direct control so I can't modify the Insert statement. The computed column might be the answer though... I'll look into that.

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-24 : 15:10:16
You're welcome, glad to help.

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

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-24 : 15:17:15
you don't haves access to the code, but you have access to ALTER a table?

"I don't think so..." -- J.S.

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-24 : 15:36:53
Good point, Brett.

You should only alter the table to add a computed column if the application is using stored procedures and * isn't used. Otherwise, you'll break the code.

An instead of trigger is really your only option in this case.

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

Subscribe to my blog
Go to Top of Page

gjohll
Starting Member

3 Posts

Posted - 2010-09-24 : 22:55:56
Adding a computed column works.

This is using a DNN module DotNetMushroom. I can create/alter the tables through their module's GUI, but in the background they add some proprietary fields and take care of the inserts etc. Sometimes one is able to access their fields, sometimes not. So, when I ran into an instance of not being able to use one of their fields for filtering records being displayed, I figured at first using a trigger to copy their field into one that I could create (through the GUI). The computed column worked instead. THANKS!

This isn't the way I'd do things if I had more of a say, but given what I've been dealt it's a work around to move the project further.

Thanks again.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-25 : 00:18:21
Cool, glad it works then.

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

Subscribe to my blog
Go to Top of Page
   

- Advertisement -