Solved

Trigger to update the same table, plus update another table

Posted on 2004-09-16
6
320 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

696 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