1

These are the columns (simplifying):

myDate as Date
theMonths as int

Then I create a computed column finalDate in Microsoft SQL Server Management Studio 2008 R2, with this formula:

(dateadd(month, theMonths, myDate))

Very simple. But there is an error:

Error validating formula

The error occurs only if I insert the formula through the integrated assistant in Management Studio. Not occurs when create column through T-SQL query.

If I change myDate type to Datetime, there's no error, but I can't change the table structure.

I try this:

(dateadd(month, theMonths, CONVERT(Datetime, myDate)))

The error persists.

I created the column directly in T-SQL using the following:

ADD [finalDate] AS (dateadd(month,[theMonths],[myDate])) PERSISTED

It works fine, the column is created, the data is computed when insert-update. The problem is in the Management Studio.

4

1 回答 1

1

试试这个——

CREATE TABLE dbo.[test]
(
    myDate DATE NOT NULL DEFAULT (GETDATE()),
    theMonths INT NOT NULL DEFAULT ((0)),
    finalDate AS (DATEADD(MONTH, theMonths, myDate)) PERSISTED
)

INSERT INTO dbo.[test] (myDate, theMonths)
VALUES('20130101', 5) 

SELECT * FROM dbo.[test]
于 2013-05-13T05:24:59.060 回答