SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


BEFORE TRIGGER issue


BEFORE TRIGGER issue

Author
Message
lassell
lassell
SSC Veteran
SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)

Group: General Forum Members
Points: 203 Visits: 432
I know that we don't have access to a MySQL BEFORE trigger but I have a strange issue that I need to resolve.

Basically, I have a requirement for truncated data to be output into a new table (un-truncated) and the original data to have a * placed at the end. However, I cannot capture the original string before it gets truncated. Below are my scripts:

1) Table one, to contain truncated '*' data:
CREATE TABLE [dbo].[StudentDetails](
[StudentID] [int] NOT NULL,
[StudentName] [varchar](15) NULL,
PRIMARY KEY CLUSTERED

2) Table two to contain 'FULL' string
CREATE TABLE [dbo].[StudentDetails_Extended](
[StudentID] [int] NOT NULL,
[StudentName] [nvarchar](400) NULL,
PRIMARY KEY CLUSTERED

3) I have tried to create the following trigger:
CREATE TRIGGER Test_Trg
ON StudentDetails

INSTEAD OF INSERT
--AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
SET ANSI_WARNINGS OFF
INSERT INTO StudentDetails (StudentID, [StudentName])
SELECT StudentID, LEFT([StudentName],10) + '*'
FROM inserted

INSERT INTO StudentDetails_Extended (StudentID, [StudentName])
SELECT StudentID, [StudentName]
FROM inserted
SET ANSI_WARNINGS on

END
GO

4) Then running the below:
SET ANSI_WARNINGS OFF
INSERT INTO [AdventureWorks].[dbo].[StudentDetails]
([StudentID]
,[StudentName])
VALUES
(533
,'MORE THAN 15 CHARS ARE INCLUDED')
GO
SET ANSI_WARNINGS ON

5) I need to see from StudentDetails.StudentName = 'MORE THAN* and from StudentDetails_Extended.StudentName = 'MORE THAN 15 CHARS ARE INCLUDED'

Is this possible?

Thanks for any help in advance, this is driving me nuts :-)
Roshan Jospeh
Roshan Jospeh
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3097 Visits: 2093
I think you need to find a diffferant way to acheive this. Inserted virtual table has the same definition as of the real table so the studentname column will capture only 15 vcaharcters in the virtual table. So your second table also will contain only 15 characters

----------------------------------------------------------------------------------------------------------------------------------------------------
Roshan Joe

Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help
lassell
lassell
SSC Veteran
SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)

Group: General Forum Members
Points: 203 Visits: 432
Thanks.

I think what I may have to do is extended the original tables field.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61529 Visits: 17954
Instead of using triggers for this could you create an INSERT sproc that receives parameters with more characters? That would seem to be a little less kludged than making the columns allow for more characters and truncating them with an instead of trigger.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Avi1
Avi1
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 67
I think there are 3 ways to achieve this in current scenario
1. Increase the size os column [StudentName] to varhcar(400), in [dbo].[StudentDetails] table same as in extended table, trigger should work fine

2. Rather then writing insert into studentdetail table, write in extended table and write instead of trigger on this table. and in the trigger insert in studentdetail table. at this point you'll get full string that is passed.

3. Instead of using trigger use stored procedure and pass values as parameter and insert in both tables separately.

hope these options will work. according to me option 3 is best and extendable

Avinash
lassell
lassell
SSC Veteran
SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)

Group: General Forum Members
Points: 203 Visits: 432
Thanks guys.

This is for a 'sunset' application/db that is being replaced by a slow migration of functionality rather than a big bang. As a consequence we cant change the front end and have limited budget for any changes to the old system, hence my problem.

Bit of a hack I know, but I have proposed the trigger solution and solved the issue I had with extending the original column size.

If anyone can think of another way please let me know.

Thanks again. :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search