Sowmiya Pr
Sowmiya Pr

Reputation: 47

SQL table names with spaces

I have a table name with spaces between say like 'Orders Details'.

I want to use that table in stored procedures with joins.I have tried using alias names,[],`` in queries but nothing seems to be working.Can anybody help me on this error!

enter image description here

Upvotes: 1

Views: 2226

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269543

Do not use order as a column alias. It is a SQL keyword. I would just use o:

select o.*, od.*
from orders o join
     order_details od
     on o.orderid = od.orderid
where year(o.orderdate) = @orderyear;

Notes:

  • Your JOIN condition is on ProductId. However, that is highly suspicious. Usually such joins are on the order id. In fact, ProductId doesn't belong in Orders (usually) if there is a detail table.
  • Do not define your tables with spaces in the name. That just makes it hard to reference the names.
  • orderdate does not seem to be defined, because it has a red underline.
  • I don't recommend select *. For one thing, you will have duplicate column names. More importantly, you want to be explicit about what this code returns, particularly in a stored function or procedure.

Upvotes: 3

Related Questions