Solved

Microsoft SQL Server Column Function

Posted on 2012-03-09
5
237 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

827 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