Search code examples
c#sqlreserved-words

How to use sql defined functions as fields?


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)
)

Solution

  • 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)
    )