Solved

Trigger to update the same table, plus update another table

Posted on 2004-09-16
6
319 Views
Last Modified: 2008-03-06
Here is my trigger so far :

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)

-- now take the same data and put it in a new table called [tStatsUsr2]
0
Comment
Question by:jkt182
[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
  • 4
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 500 total points
ID: 12079196
you need to be specific about what you want, but this is the geberal way to do it :
Be warned, this type of trigger will not work if you do a set-based insert. It will only work if you insert one row at a time.

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
DECLARE @RND integer
SET @RND = RAND() * 1000
DECLARE @NEWVAR varchar(6)
SET @NEWVAR = @coupon + RIGHT('000' + cast(@RND AS VARCHAR(3)))
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)
UPDATE [tStatsUsr] SET ?WHATEVER_COLUMN_YOU_WANT_TO_UPDATE?  = @NEWVAR
WHERE useName = (SELECT useName FROM INSERTED)
-- now take the same data and put it in a new table called [tStatsUsr2]
INSERT INTO [tStatsUsr2] (COL1, COL2,....) VALUES (@NEWVAR, whatever columns you want......)
0
 

Author Comment

by:jkt182
ID: 12079377
Here is my updated Trigger :

I get this error : ERROR 174 : The Right Function Requires 2 Arguments

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
DECLARE @RND integer
SET @RND = RAND() * 1000
DECLARE @NEWVAR varchar(6)
SET @NEWVAR = @coupon + RIGHT('000' + cast(@RND AS VARCHAR(3)))
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)
UPDATE tStatsUsr SET usr  = @NEWVAR
WHERE useName = (SELECT usrName FROM INSERTED)
-- now take the same data and put it in a new table called [tStatsUsr2]
INSERT INTO Coupons (couponCode) VALUES (@NEWVAR)
0
 

Author Comment

by:jkt182
ID: 12079439
Ok, i figured out the error, and this works !

Thank you so much !
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:jkt182
ID: 12079444
here is my final trigger in case others need a real working example :

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
DECLARE @RND integer
SET @RND = RAND() * 1000
DECLARE @NEWVAR varchar(6)
SET @NEWVAR = @coupon + RIGHT('000' + cast(@RND AS VARCHAR(3)),2)
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)
UPDATE tStatsUsr SET usr  = @NEWVAR
WHERE usrID = (SELECT usrID FROM INSERTED)
-- now take the same data and put it in a new table called [tStatsUsr2]
INSERT INTO Coupons (couponCode) VALUES (@NEWVAR)

0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12079476
close!! but if you want a 3 digit rand number, then take RIGHT('000' + cast(@RND AS VARCHAR(3)),3)
0
 

Author Comment

by:jkt182
ID: 12079771
Thanks ! You've been a great help again !
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

738 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