-1

Is there someone who can help me?

I have to do a query in a database which was created by someone else. The problem I encounter is that the name of the table contains three spaces.

Example of the query: "SELECT * FROM Item Journal Line".

It does not work anyway, I've tried by surrounding the tablename with [], `, ´´, '',´ but nothing is working.

Any suggestion on how to address this table in my query? I've searched on the web but I did not find anything which could help me. I'm working in c# with SharpDevelop.

Terry
  • 5,132
  • 4
  • 33
  • 66
pidel
  • 65
  • 9

4 Answers4

3

If you table name is test table

In MSSQL (which i guess this is what you are using) run

select * from "test table"

To create this string in C# use Escape Sequences, and escape " with\"

string queryStr = "select * from  \"test table\" " ;
Mzf
  • 5,210
  • 2
  • 24
  • 37
2

The sanctioned way to access a Sql Server database table containing spaces or other reserved characters is through the use of square brackets. But you should not include the dbo part in that otherwise the search will fail because there is no table with dbo as first 3 characters.

string strConnection = "server=***;" + "database=***;" + "uid=***;" + "pwd=***;"; 
string query = "SELECT * FROM [Item Journal Line]";
using(SqlConnection myConnection = new SqlConnection(strConnection))
using(SqlCommand command= new SqlCommand(query, myConnection))
{ 
     myConnection.Open(); 
     SqlDataReader reader = command.ExecuteReader() 
}

However, if your really have tables with so weird names, consider to create a view with more easier names to work with

Steve
  • 213,761
  • 22
  • 232
  • 286
1

In Postgresql

 String query= "select * from appsetup.""fsd sdfsd sdfds"""

pass this query in `command` 
Sathish
  • 4,419
  • 4
  • 30
  • 59
0

Dave Rook said it in your comments.

The answer to the question you asked is over here: Query a table that has spaces in its name

        string sqlQuery = "SELECT * from [Item Journal Line]";  

Did this solve your problem? Maybe someone added a space or something evil in the name of your table.

        string sqlQuery = "SELECT * from [Item Journal Line /n]"; 

If this isn't solving your problem it could be something else besides what you have asked, can you post the error message or tell us if the problem is resolved?

Community
  • 1
  • 1
JPK
  • 1,324
  • 2
  • 14
  • 25