How to work with Sql table named Group

I have a table called Group in my database, when I try to work on it, it gives an error:

SELECT GroupID,GroupName FROM Group 

he said:

  Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Group'. 

I know that Group is an illegal name for a table, but its an old database with too much data and relationships, and I cannot rename this table, is there any way to work with the table?

+5
source share
4 answers

You can use square brackets [ and ] in the column name, table name.

 SELECT GroupID,GroupName FROM [Group] 

others, for example.

 select * from [table] select [primary] from [table] 

PS: it worked by adding [dbo] to [Group], so now it looks like this:

  SELECT GroupID, GroupName FROM [dbo].[Group] 
+10
source
 SELECT GroupID, GroupName FROM `Group` 

Now he knows that Group is the name of the table, not the incorrectly placed language syntax

+2
source

It always requires quotation marks and becomes case sensitive

So:

 SELECT GroupID,GroupName FROM "Group" 

if the table name is a group, then:

 SELECT GroupID,GroupName FROM "Group" 

Be sure to include a name.

Although it is not recommended to use table names that require quotation marks and are reserved words.

If you can change the name of the table.

+1
source

You can use the database name before the table name or you can give it an alias.

 select t1.col1 from db1.table1 t1 

t1 - table attribute

db1 - database name

0
source

Source: https://habr.com/ru/post/1205843/


All Articles