仅在我添加数据库名称时才进行特定选择工作

时间:2011-04-23 09:32:31

标签: sql-server-2008 select

我正在使用SQL Server 2008 Express

所有选择的陈述都没问题。现在我有了这个:

SELECT 
   ORG.id, ORG.img, ORG.name, ORG.city, ORG.address, 
   ORG.zip, ORG.telephone, ORG.telephone2, 
   ORG.fax, ORG.email, ORG.vaname, ORG.vanumber, 
   ORG.yor_photo, ORG.commission, 
   Clients.id AS yor_id, Clients.prefix, 
   Clients.fname, Clients.lname, Clients.phone, 
   Clients.pelephone, Clients.email, Clients.pid, 
   ORG.adddate, ORG.note
FROM Org 
LEFT OUTER JOIN 
    (select * from Clients where yor = 1) as Clients ON Clients.company = ORG.id 
WHERE ORG.id=" & ORGID

它无效,我收到错误“无效对象名称”

如果我在表名前面添加DBNAME.DBO,那么它可以工作

问题是我不想在每个项目上改变它

为什么不起作用?

更新

问题不在于数据库名称,问题在于选择语句中的 AS yor_id 。 如果我删除它,检索到的记录不是所有数据都满,但如果我写它数据已满,但yor_id为空

更新

永远不会,我的坏! id列已被破坏

1 个答案:

答案 0 :(得分:1)

在该查询之前,您需要编写

use DBNAME

以切换到您的dbname。

您可能正在master数据库上运行该查询,所以只需执行此操作即可:

use DBNAME

SELECT 
   ORG.id, ORG.img, ORG.name, ORG.city, ORG.address, 
   ORG.zip, ORG.telephone, ORG.telephone2, 
   ORG.fax, ORG.email, ORG.vaname, ORG.vanumber, 
   ORG.yor_photo, ORG.commission, 
   Clients.id AS yor_id, Clients.prefix, 
   Clients.fname, Clients.lname, Clients.phone, 
   Clients.pelephone, Clients.email, Clients.pid, 
   ORG.adddate, ORG.note
FROM Org 
LEFT OUTER JOIN 
    (select * from Clients where yor = 1) as Clients ON Clients.company = ORG.id 
WHERE ORG.id=" & ORGID