Solved

Microsoft SQL Server Column Function

Posted on 2012-03-09
5
234 Views
Last Modified: 2012-03-13
I'm trying to set the default value in a table I'm creating to:
(dateadd(s,TotalTime*60, StartTime))

Where TotalTime is another column and is a Real.
And StartTime is another column and is a DateTime.

Syntax help please.

Thanks,
Ni
0
Comment
Question by:KnightWhoSaysNi
5 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 300 total points
ID: 37704176
I don't believe that's possible since SQL won't let you use field names in the default value.

You'll probably need to create a trigger to handle this. Something like...

CREATE TRIGGER
ON tableName
AFTER INSERT,UPDATE
AS
UPDATE tableName SET dateColumn = dateadd(s,TotalTime*60, StartTime) WHERE primaryKeyField IN (select primaryKeyField from inserted where dateColumn IS NULL)

Open in new window

0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 100 total points
ID: 37710137
A computed column may also be an option - Of course it depends on your requirment
Do you want the field to have a default value that can be overwritten if so desired by an app/users
If not a computed column might be an option
ALTER TABLE yourtable ADD yourcolumn (dateadd(s,TotalTime*60, StartTime))
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 100 total points
ID: 37712709
As suggested previously you need a computed column, just a minor correction (no points please), for a computed column the AS is required as in:
ALTER TABLE yourtable ADD yourcolumn AS (DATEADD(s,TotalTime * 60, StartTime))
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37712969
Yes - as bcunney said, It depends on your requirement. Since you said you want to set a "default" value I assume you're wanting to be able to insert data into the column.  You can't insert data into a computed column using an INSERT or UPDATE statement. If that's what you want you'll need a trigger. If you don't need to insert data then the computed column is the better and simpler option.
0
 

Author Closing Comment

by:KnightWhoSaysNi
ID: 37714653
Thanks for the input.  I think either solution provided would resolve the issue, but I went a little different direction.

I decided to use a stored procedure to perform the insert and declare the calculated variables in the stored procedure.

BEGIN TRY
      DECLARE @StartTime datetime;
      DECLARE @EndTime datetime;
      SET @EndTime = GETDATE();
      SET @StartTime = DATEADD(SS,@TotalTime*-60,@EndTime);
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

746 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

8 Experts available now in Live!

Get 1:1 Help Now