Solved

Trigger to update the same table, plus update another table

Posted on 2004-09-16
6
315 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now