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

Narendra gravatar 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

Madhivanan gravatar image

Madhivanan
1.1k 1 3 6

(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

Kristen gravatar image

Kristen ♦
2.2k 6 7 10

(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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:

x1834
x22
x19

asked: Oct 26, 2009 at 10:45 AM

Seen: 3537 times

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