1

I am creating tables in Sql Management Studio 2012 using SQL. How do I make fields or columns with names that are already defined in Sql Server e.g User_ID, User_Name. I want to use them as fields in my tables.

Code

Table definition from Duplicate Post:

create table Ticket(
  Ticket_Id varchar(10) not null,
  TicketType_Id varchar(3) not null,
  Ticket_PurchaseDate DateTime null,
  LottoDraw_Id int null,
  User_Id int null,
  Ticket_IsWinner bit null
  Primary Key(Ticket_Id,TicketType_Id)
)
Community
  • 1
  • 1
  • 5
    Please post your code as text not a link to an image – Steve May 10 '15 at 12:41
  • Use SQLDataAdapter to load SQL Select query into a DataTable. Fields in DataTable can be indexed either by index number or by indexed by string name which is the SQL Field name. – jdweng May 10 '15 at 12:49
  • 3
    You can escape keywords using by surrounding them with square brackets e.g. `[name]`. – T I May 10 '15 at 14:28
  • One way is to follow T I's suggestion, and another is simply not to do it. there is a reason why reserved words are reserved. [read this](http://stackoverflow.com/questions/30131409/is-there-a-way-to-not-use-square-brackets-in-sql-server/30132058) – Zohar Peled May 10 '15 at 14:33
  • 3
    None of those are [reserved words](https://msdn.microsoft.com/en-us/library/ms189822.aspx). The code you posted works fine without any escaping. – Martin Smith May 10 '15 at 15:46
  • 1
    Possibly OP was unnerved by the syntax highlighting in the pasted image. – StuartLC May 11 '15 at 07:15

1 Answers1

2

Warp the column name like in brackets [ ] ... such as

create table Ticket(
  Ticket_Id varchar(10) not null,
  TicketType_Id varchar(3) not null,
  Ticket_PurchaseDate DateTime null,
  LottoDraw_Id int null,
  [User_Id] int null,
  Ticket_IsWinner bit null
  Primary Key(Ticket_Id,TicketType_Id)
)
Nazmul
  • 575
  • 3
  • 18
  • While this answers the question exactly, I would recommend avoiding this kind of naming convention in sql. I've already linked my reasons in a comment to the question, so I won't do it again here. – Zohar Peled May 10 '15 at 15:44