CREATE TABLE [dbo].[tblLocations](
[latitude] [float] NOT NULL,
[longitude] [float] NOT NULL,
[location] [varchar](500) NOT NULL,
[timestamp] [datetime] NOT NULL,
[point] [geography] AS geography::Point(latitude, longitude, 4326) NOT NULL
)
Giving me bad syntax at the word AS
.
Isn't this how you declare a computed column?
Go to your database, right click on tables, select “New Table” option. Create all columns that you require and to mark any column as computed, select that column and go to column Properties window and write your formula for computed column.
Get a list of computed columns in a SQL Server database. We can use the system function sys. computed_columns and join it with the sys. objects to get a list of available computed columns, their data type, and the column definition (formula).
When altering a computed column the only thing you can do is drop it and re-add it. Show activity on this post. This is one of those situations where it can be easier and faster to just use the diagram feature of SQL Server Management Studio.
A persisted computed column is one that is physically stored in the table. If you don't specify that it's persisted, then the column's value will be calculated each time you run a query against it. You can query the sys. computed_columns system catalog view to find out whether a computed column is marked as persisted.
You don't declare the datatype
or nullability yourself
CREATE TABLE [dbo].[tblLocations](
[latitude] [float] NOT NULL,
[longitude] [float] NOT NULL,
[location] [varchar](500) NOT NULL,
[timestamp] [datetime] NOT NULL,
[point] AS geography::Point(latitude, longitude, 4326)
)
In general SQL Server will assume the column is nullable unless you add an ISNULL()
around the formula.
However I just tried the following column definition
[point2] AS ISNULL(geography::Point(latitude, longitude, 4326),
geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326))
and that still shows up as is_nullable
in sys.computed_columns
so it doesn't look like that applies to CLR datatypes (probably as SQL Server doesn't trust these to be deterministic).
Edit: However it is syntactically valid to specify NOT NULL
as long as the computed column is marked as PERSISTED
i.e.
[point] AS geography::Point(latitude, longitude, 4326) PERSISTED NOT NULL
In this particular case however attempting to create a table with such a definition gives a runtime error.
Computed column 'point' in table 'foo' cannot be persisted because the column type, 'geography', is a non-byte-ordered CLR type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With