question

Narendra avatar image
Narendra asked

What is the syntax to create a table variable?

CREATE TABLE @username (name nvarchar(50))

Please suggest any option.

sql-server-2008table-variabletemporary-tables
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Madhivanan avatar image
Madhivanan answered

It should be either

CREATE TABLE username (name nvarchar(50))            

or

DECLARE @username TABLE (name nvarchar(50))            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kristen avatar image
Kristen answered

As it is not possible to modify a Table Variable once created, and it can only have one index - the Primary Key - we always [well, nearly always!] add a PK in the declaration:

DECLARE @username TABLE            
(            
    [name] nvarchar(50) NOT NULL,            
    ... any other columns ...            
    PRIMARY KEY            
    (            
        [name]            
    )            
)            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.