Sunday, March 25, 2012

Derived Column in CREATE TABME

Hi champs,
I am kicking my self for the syntax for a derived column in a CREATE TABME
-statement.
I just wnat a extra colum that is the result of colum1+colum2
..
CREATE TABLE [dbo].[test](
[colum1] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[colum2] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[colum3] = [colum1] + [colum2] [nchar](10)
) ON [PRIMARY]
what is the correct syntax?
/many thanksKurlan
CREATE TABLE [dbo].[test](
[colum1] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[colum2] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[colum3] AS [colum1] + [colum2]
) ON [PRIMARY]
"Kurlan" <Kurlan@.discussions.microsoft.com> wrote in message
news:7D8CBBB0-4538-47C9-833F-450D689879E2@.microsoft.com...
> Hi champs,
> I am kicking my self for the syntax for a derived column in a CREATE TABME
> -statement.
> I just wnat a extra colum that is the result of colum1+colum2
> ..
> CREATE TABLE [dbo].[test](
> [colum1] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
> [colum2] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
> [colum3] = [colum1] + [colum2] [nchar](10)
> ) ON [PRIMARY]
> what is the correct syntax?
>
> /many thanks|||CREATE TABLE [dbo].[test](
[colum1] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[colum2] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[colum3] as [colum1] + [colum2]
) ON [PRIMARY]
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/
"Kurlan" wrote:

> Hi champs,
> I am kicking my self for the syntax for a derived column in a CREATE TABME
> -statement.
> I just wnat a extra colum that is the result of colum1+colum2
> ..
> CREATE TABLE [dbo].[test](
> [colum1] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
> [colum2] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
> [colum3] = [colum1] + [colum2] [nchar](10)
> ) ON [PRIMARY]
> what is the correct syntax?
>
> /many thanks|||Kurlan a écrit :
> Hi champs,
> I am kicking my self for the syntax for a derived column in a CREATE TABME
> -statement.
> I just wnat a extra colum that is the result of colum1+colum2
> ..
> CREATE TABLE [dbo].[test](
> [colum1] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
> [colum2] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
==> [colum3] = [colum1] + [colum2] [nchar](10)
> ) ON [PRIMARY]
> what is the correct syntax?
>
> /many thanks
The SQL type must not be explicit. It will be implicit by calculating
the SQL type needed by the expression.
Remember that this type of columns are computed while running the SELECT.
A +
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************|||>> CREATE TABLE [dbo].[test](
>==> [colum3] = [colum1] + [colum2] [nchar](10)
>The SQL type must not be explicit. It will be implicit by calculating
>the SQL type needed by the expression.
If, for some reason, there is a requirement for an explicit data type,
this can be achieved by adding a CONVERT to the expression:
[colum3] = CONVERT(nchar(10), [colum1] + [colum2])
Roy Harvey
Beacon Falls, CTsql

No comments:

Post a Comment