Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

BEFORE TRIGGER issue Expand / Collapse
Author
Message
Posted Sunday, May 12, 2013 7:25 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 2:44 AM
Points: 50, Visits: 306
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
Post #1451958
Posted Sunday, May 12, 2013 9:15 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 6:50 AM
Points: 617, Visits: 1,278
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
Custom cleanup script for backups
Post #1451962
Posted Sunday, May 12, 2013 9:19 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 2:44 AM
Points: 50, Visits: 306
Thanks.

I think what I may have to do is extended the original tables field.


Post #1451963
Posted Monday, May 13, 2013 7:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:59 PM
Points: 13,007, Visits: 12,426
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 Moden's 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)
Post #1452097
Posted Monday, May 13, 2013 8:10 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 5:16 PM
Points: 94, Visits: 45
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
Post #1452124
Posted Monday, May 13, 2013 3:29 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 2:44 AM
Points: 50, Visits: 306
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.
Post #1452339
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse