简体   繁体   English

SQL 表名带空格

[英]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!我想在带有连接的存储过程中使用该表。我尝试在查询中使用别名,[],`` 但似乎没有任何效果。有人可以帮我解决这个错误!

在此处输入图像描述

Do not use order as a column alias.不要使用order作为列别名。 It is a SQL keyword.它是一个 SQL 关键字。 I would just use o :我只会使用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 .您的JOIN条件在ProductId上。 However, that is highly suspicious.然而,这是非常可疑的。 Usually such joins are on the order id.通常,此类连接位于订单 ID 上。 In fact, ProductId doesn't belong in Orders (usually) if there is a detail table.事实上,如果有明细表, ProductId不属于Orders (通常)。
  • 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. orderdate似乎没有定义,因为它有一个红色下划线。
  • I don't recommend select * .我不推荐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.更重要的是,您希望明确说明此代码返回的内容,尤其是在存储的 function 或过程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM