x
login about faq Site discussion (meta-askssc)

What is the syntax to create a table variable?

CREATE TABLE @username (name nvarchar(50))

Please suggest any option.

more ▼

asked Oct 26 '09 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 '09 at 10:52 AM

Madhivanan gravatar image

Madhivanan
1.1k 1 2 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 '09 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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1601
x18
x16

asked: Oct 26 '09 at 10:45 AM

Seen: 1940 times

Last Updated: Oct 26 '09 at 01:40 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.