Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Setting a formula in a table during create

Author  Topic 

vavs
Starting Member

24 Posts

Posted - 2010-09-02 : 12:36:01
I am creating a table that will serve multiple purposes. I need to have one of the fields to be a calculation of two other fields. After I created the table using the following code:
/*
SELECT
TAG#
, dbo.FRINFM.GRADE
, Gauge
, Width
, Weight
, CAST(Weight/2000.00 AS FLOAT) AS Tons
, Status
, CAST([Cost-Cwt] AS DECIMAL(4,2)) AS [Cost-Cwt]
, dbo.tblGrades.Type AS GaugeType
, SPACE(2) AS TYPE
, CAST(0.00 AS FLOAT) AS DecIn
, CAST(0.00 AS FLOAT) AS DecOut
, CAST(0.00 AS FLOAT) AS DecAvg
, SPACE(6) AS GaugeFinal
, CAST(Weight*[Cost-Cwt]/100 AS MONEY) AS InvValue

INTO dbo.TMIINVa
FROM dbo.FRINFM
JOIN dbo.tblGrades ON dbo.FRINFM.GRADE = dbo.tblGrades.Grade
WHERE
[status] IN (3, 4)
*/

I went back and changed DecAvg to include the formula DecIn+DecOut/2
I changed it in the table design under columns,formula.

Is there a way to add that to the above?

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-09-03 : 00:12:34
No, a computed column cannot be added using SELECT INTO statement. You can read more under limitations & restrictions here:

>>http://msdn.microsoft.com/en-us/library/ms188029.aspx

It also says you cant use ALTER COLUMN on it in place, so either don't add in your SELECT INTO, or drop and recreate it post table creation.

>>http://msdn.microsoft.com/en-us/library/ms190273.aspx

Go to Top of Page
   

- Advertisement -