views:

68

answers:

2

I am linked to a Proficy Historian that allows periods in the column names. Because the data is stored in a non DBMS format I can not use openquery to get the data because there is no set schema to the tables. So I must use four part name syntax to get the data. This example works:

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

but this fails with Incorrect 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 name of the column

This fails as well with Incorrect syntax near the keyword 'from'.

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

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

EDIT:

Martins suggestion of the right brackets to escape the brackets work only on the outside of the sql call

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

However it does not work inside Incorrect syntax near the keyword 'from'.

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 had to escape the column escape :)

+3  A: 

(Edited to reflect new knowledge, if you like this vote for Martin Smith's answer instead!)

Escape the ] by 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
Andomar
`Incorrect syntax near the keyword 'from'.`Seems to blow up the sql call to have that many brackets in there. Different error though. Normally the command `SELECT [SERVER.pid_astatus[07][0].F_CV.Value] from ihTrend` in the brackets is all black with no syntax highlighting, but after I did what you suggested the syntax highlighting starts at the `from` to the end of the command
efbenson
+5  A: 

You only need to escape these ]

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

This works for me

CREATE TABLE #t(
    [pid_astatus[07]][0]].F_CV.Value] int
) 

SELECT [pid_astatus[07]][0]].F_CV.Value]
FROM #t
Martin Smith
If I test this with `declare @t table ([[[a]]] int) select [[a]]] from @t`, it gives an error. Same for basically any other combination except `select [[[a]]]`
Andomar
@Andomar - Does the code I just posted work for you?
Martin Smith
+1 Yeah, and `select * from syscolumns where id = OBJECT_ID(...)` confirms that my column name `[[[a]]]` is really named `[[a]`. Good catch!
Andomar
Martin thanks for the help so far, but as stated above it does not work within the main body of the []s.
efbenson
@efbenson: Did you try the second part of my answer? Your use of a subquery in the `table` part of `[server].[db].[schema].[table]` is kind of weird, I'm surprised it works with `select *` !
Andomar
Do quoted identifiers work? "SERVER.pid_astatus[07][0].F_CV.Value". Ah Just seen Andomar's comment as well. I wasn't familiar enough with the linked server syntax to comment on that.
Martin Smith
@Andomar unfortunaly the way that the linked server translates its data to RDBMS does not work with your second suggestion. If it supported more features I am sure it would work.
efbenson