x

What is the syntax to create a table variable?

CREATE TABLE @username (name nvarchar(50))

Please suggest any option.

more ▼

asked Oct 26, 2009 at 10:45 AM in Default

avatar image

Narendra
11 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

It should be either

CREATE TABLE username (name nvarchar(50))            

or

DECLARE @username TABLE (name nvarchar(50))            
more ▼

answered Oct 26, 2009 at 10:52 AM

avatar image

Madhivanan
1.1k 2 5 9

(comments are locked)
10|1200 characters needed characters left

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]            
    )            
)            
more ▼

answered Oct 27, 2009 at 07:09 AM

avatar image

Kristen ♦
2.2k 7 11 14

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x2091
x27
x23

asked: Oct 26, 2009 at 10:45 AM

Seen: 4230 times

Last Updated: Oct 26, 2009 at 01:40 PM

Copyright 2016 Redgate Software. Privacy Policy