BEFORE TRIGGER issue

  • 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 🙂

  • 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[/url]

  • Thanks.

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

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

  • 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. 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply