Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT query on a table with a space in the name using SQSH

I'm using SQSH (version 2.1) on Ubuntu 10.04 to connect to a MSSQL database using a command like this:

sqsh -S server -U user -P password -D database

I have a table called My Table, but I cannot find a way to run a SELECT query on it. This is what I've tried so far:

SELECT * FROM 'My Table'
go

Output: Incorrect syntax near 'My Table'. (I get the same for double quotes)

\set t="My Table"
SELECT * FROM $t
go

Output: Invalid object name 'My'. (Which is weird because if I do \echo $t, I get the full table name)

SELECT * FROM My\\ Table
go

Output: Invalid object name 'My'.

SELECT * FROM [My Table]
go

Output: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.

This last command works fine for table names without any spaces.

UPDATE: other commands work fine e.g. I can get the table description with:

SELECT column_name,data_type FROM information_schema.columns WHERE table_name = 'My Table'
go
like image 573
jackocnr Avatar asked Jan 19 '12 13:01

jackocnr


2 Answers

Putting the table name in quotes doesn't work in MS SQL Server.
The correct way is using [ ]:

SELECT * FROM [My Table]
like image 96
Christian Specht Avatar answered Nov 13 '22 11:11

Christian Specht


Im using SQL 2008R2, and the following works for me

['table name']
like image 4
Shane Avatar answered Nov 13 '22 12:11

Shane