question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mssql - incorrect and incomplete DDL for tables with calculated columns and extended properties

See original GitHub issue

System information:

  • Operating system (distribution) and version: Windows 10
  • DBeaver version: .3.5.202102141704
  • Additional extensions

Connection specification:

  • Database name and version: SQL Server 2019
  • Driver name: information not available in connection properties
  • Do you use tunnels or proxies (SSH, SOCKS, etc)?

Describe the problem you’re observing:

  • calculated columns are shown as real colums
  • extended properties are missing

Steps to reproduce, if exist:

create this table in SQL Server

CREATE TABLE [repo].[Parameter](
	[Parameter_name] [varchar](100) NOT NULL,
	[sub_Parameter] [nvarchar](128) NOT NULL,
	[Parameter_desciption] [nvarchar](1000) NULL,
	[Parameter_default_value] [sql_variant] NULL,
	[Parameter_value] [sql_variant] NULL,
	[Parameter_value__result_nvarchar]  AS (TRY_CAST(coalesce([Parameter_value],[Parameter_default_value]) AS [nvarchar](4000))),
	[Parameter_value__result_int]  AS (TRY_CAST(coalesce([Parameter_value],[Parameter_default_value]) AS [int])),
 CONSTRAINT [PK_Parameter] PRIMARY KEY CLUSTERED 
(
	[Parameter_name] ASC,
	[sub_Parameter] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [repo].[Parameter] ADD  DEFAULT ('') FOR [sub_Parameter]
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'11f37926-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'Parameter_name'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'12f37926-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'sub_Parameter'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObject_guid', @value=N'69cbc49c-3862-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'CONSTRAINT',@level2name=N'DF__Parameter__sub_P__18B6AB08'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'13f37926-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'Parameter_desciption'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'14f37926-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'Parameter_default_value'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'15f37926-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'Parameter_value'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'16f37926-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'Parameter_value__result_nvarchar'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObjectColumn_guid', @value=N'75b33a4a-426d-eb11-84e2-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'COLUMN',@level2name=N'Parameter_value__result_int'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObject_guid', @value=N'2690291c-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter', @level2type=N'CONSTRAINT',@level2name=N'PK_Parameter'
GO

EXEC sys.sp_addextendedproperty @name=N'RepoObject_guid', @value=N'2490291c-9d61-eb11-84dc-a81e8446d5b0' , @level0type=N'SCHEMA',@level0name=N'repo', @level1type=N'TABLE',@level1name=N'Parameter'
GO

compare with DDL, shown in dbeaver:

-- Drop table

-- DROP TABLE dhw_self.repo.[Parameter] GO

CREATE TABLE dhw_self.repo.[Parameter] (
	Parameter_name varchar(100) COLLATE Latin1_General_CI_AS NOT NULL,
	sub_Parameter nvarchar(128) COLLATE Latin1_General_CI_AS DEFAULT '' NOT NULL,
	Parameter_desciption nvarchar(1000) COLLATE Latin1_General_CI_AS NULL,
	Parameter_default_value sql_variant NULL,
	Parameter_value sql_variant NULL,
	Parameter_value__result_nvarchar nvarchar(4000) COLLATE Latin1_General_CI_AS NULL,
	Parameter_value__result_int int NULL,
	CONSTRAINT PK_Parameter PRIMARY KEY (Parameter_name,sub_Parameter)
) GO

missing extend properties could be OK But the calculated columns needs to be calculated columns

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aisbergdecommented, Feb 26, 2021

@kseniiaguzeeva You could delete (ignore) WITH ..., It does not matter for the calculated columns

the script is originally created when I create a script for any table directly in SSMS (SQL Server Management Studio)

This should be fine to test:

CREATE TABLE [repo].[Parameter](
	[Parameter_name] [varchar](100) NOT NULL,
	[sub_Parameter] [nvarchar](128) NOT NULL,
	[Parameter_desciption] [nvarchar](1000) NULL,
	[Parameter_default_value] [sql_variant] NULL,
	[Parameter_value] [sql_variant] NULL,
	[Parameter_value__result_nvarchar]  AS (TRY_CAST(coalesce([Parameter_value],[Parameter_default_value]) AS [nvarchar](4000))),
	[Parameter_value__result_int]  AS (TRY_CAST(coalesce([Parameter_value],[Parameter_default_value]) AS [int])),
 CONSTRAINT [PK_Parameter] PRIMARY KEY CLUSTERED 
(
	[Parameter_name] ASC,
	[sub_Parameter] ASC
)
) ON [PRIMARY]

You can also use any other table containing any calculated column.

BTW, the code above contains “normal” (not persisted) calculated column. But calculated columns in SQL Server can be also persisted. And it is important to keep this information, if it is persisted or not.

Full syntax: https://docs.microsoft.com/de-de/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver15

This is the part for a calculated column

<computed_column_definition> ::=
column_name AS computed_column_expression
[ PERSISTED [ NOT NULL ] ]
...

You can ignore the NOT NULL, it will be implicitly defined, but the PERSISTED is an important option which needs to keep.

0reactions
uslsscommented, Apr 26, 2021

verified

Read more comments on GitHub >

github_iconTop Results From Across the Web

wrong DDL for tables containing extended properties on ...
This is a general issue in all my tables, because in my databases each object and each column has extended properties. CREATE TABLE...
Read more >
Database engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
An overview of computed columns in SQL Server - SQLShack
We have a property in the Computed Column Specification for the persisted column – Is Persisted. Currently, we leave it as default as...
Read more >
Defining Constraints and Indexes - SQLAlchemy Documentation
In SQLAlchemy as well as in DDL, foreign key constraints can be defined as additional attributes within the table clause, or for single-column...
Read more >
Chapter 16 SQL Data Manipulation Language
In this next example, we will use a Publishers table (Table 16.2). ... If you do not include the comma, SQL Server assumes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found