Solved

SQL Triggers

Posted on 2014-10-11
4
146 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
4 Comments
 
LVL 24

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 500 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 32

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now