?
Solved

SQL Triggers

Posted on 2014-10-11
4
Medium Priority
?
154 Views
Last Modified: 2014-10-13
Hello All,

I have two triggers that I am trying to execute.  The first script attached is supposed to get the Juilian date and calculate the individuals age based upon their date of birth.  There may be an easier way to get the correct age calculation so if there is, please advise.  The next script seems to have an issue with my inserting an image into the table, although the column has been declared with the correct datatype.  So I am not sure why this error message is being returned in MS SQL Server.  Thanks in advance for any assistance you can offer.

Regards,
aj85
Trigger-Errors.txt
0
Comment
Question by:aj85
[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 Comments
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 40375326
These triggers are not used correctly.  Usually triggers operate on the inserted and deleted tables to detect or affect other things.  The first trigger is breaking because you have a SET [AgeDays] equal to a subquery that returns more than one column.  You cannot do that in an update statement.  The SET can only set a column equal to 1 value and there is not a where clause, so if it worked, it would set the value for ALL rows in the table.

The second trigger does nothing, and the error is because of the varbinary(max) column.  These columns cannot be manipulated or retrieved from the inserted tables.  So the error is related to that.  But the statement at the bottom will do nothing to the table as you only SET @TStamp which is a variable not even part of the table you have the trigger on.

I am not sure I see the goal of these triggers.

If you can help us understand what you intend and why you chose triggers, that may help us give you the right guidance.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 2000 total points
ID: 40375518
Well, the UPDATE set agedays gets the error because you are selecting three different fields (count the number of calculate AS results)

For the second one, check the setting of @content - in fact comment that out for the moment and see if that makes the difference.

Might help to see the full triggers to see the ramifications and help with correct usage.

Be ware that some datatypes don't always convert the way you want implicitly.  So, might also help if we can see the create script for the Photos table.

And (by the way) you should be using @@DBTS to get the last used value of a TIMESTAMP column, it will be updated with the update, so if checking update(tsamp) and tsamp is a timestamp column, then there is a basic logic problem (maybe)... see : http://msdn.microsoft.com/en-us/library/ms187366(v=sql.90).aspx
0
 
LVL 35

Expert Comment

by:ste5an
ID: 40376665
I have two triggers that I am trying to execute.  The first script attached is supposed to get the Juilian date and calculate the individuals age based upon their date of birth.

This is wrong. When you read your table in three weeks, the calculated age is off by three weeks. This simply wrong.
Age calculations are always done "on-the-fly". In a view, a stored procedure or in an adhoc query. Or a calculated column.

The second trigger is logically not necessary. Cause you erase the value set by a user. I guess you want a modified date column to indicate when the *row* has changed. This is slightly different:

CREATE TRIGGER trgUpdatePhotoTStamp ON [Photos]
    FOR UPDATE
AS
    SET NOCOUNT ON;
    UPDATE  P
    SET     TStamp = CONVERT(VARBINARY, GETDATE(), 101)
    FROM    Photos P
            INNER JOIN INSERTED I ON I.primaryKeyColumns = P.primaryKeyColumns;
GO

Open in new window


But why on earth are you using a VARBINARY column?

Are you mixing the TIMESTAMP data type with the common usage of the word timestamp?
The TIMESTAMP data type in T-SQL is a binary update counter, not a date/time value.
0
 

Author Comment

by:aj85
ID: 40376957
Hello Mark,

Thanks for the link it was very helpful in assisting to solve my issue.

Regards,
aj85
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

762 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