Select a column with a period in a SQL Server column name

I am associated with Proficy Historian, which allows the use of periods in column names. Since the data is stored in a format different from the DBMS, I can not use openquery to receive data, since there is no established scheme for tables. So to get the data, I have to use a four-part name syntax. This example works:

 SELECT * FROM iHist...[SELECT * FROM ihTrend] 

but this does not work with the wrong syntax near '.'.

 SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07][0].F_CV.Value] FROM ihTrend] 

where SERVER.pid_astatus[07][0].F_CV.Value is the column name

This also does not work with the wrong syntax next to the 'from' keyword.

 SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[[07]][[0]].F_CV.Value] from ihTrend]` 

Any ideas on how I can get SQL Server to see this as a column?

EDIT:

Martins' suggestion of correct brackets to escape brackets only works outside of sql call

 SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...[SELECT * FROM ihTrend] 

However, it does not work inside the Invalid syntax next to the 'from' keyword.

 SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM ihTrend] 

EDIT

 SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value]] FROM ihTrend] 

I needed to avoid the column escape sequence :)

+4
source share
2 answers

You only need to avoid these ]

 [pid_astatus[07]][0]].F_CV.Value] 

It works for me

 CREATE TABLE #t( [pid_astatus[07]][0]].F_CV.Value] int ) SELECT [pid_astatus[07]][0]].F_CV.Value] FROM #t 
+6
source

(Edited to reflect new knowledge if you like this voice instead of Martin Smith's answer!)

Run ] , doubling them:

 SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] from ihTrend] 

Based on your comment try:

 SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...ihTrend 
+4
source

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


All Articles