仅当我添加数据库名称时特定选择才起作用

发布于 2024-11-03 00:25:42 字数 883 浏览 0 评论 0原文

我正在使用 SQL Server 2008 Express

所有的 select 语句都很好。现在我有这个:

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 列已损坏

I'm using SQL Server 2008 Express

All of the select statments are fine. Now I have this one:

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

It is not working, I get error "invalid object name"

If I add the DBNAME.DBO in front of the table name it works

The problem is that i don't want to change that on every project

Why is it not working?

UPDATE

the problem is not with the db name, the problem is with the AS yor_id in the select statment.
if i remove it the record retrieved is not full with all the data but if i write it the data is full but yor_id is empty

UPDATE

NEVER MIND, my bad! id column was corupted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-11-10 00:25:42

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

use DBNAME

以便切换到您的数据库名。

也许您正在主数据库上运行该查询,因此只需执行此操作即可:

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

Before that query you need to write

use DBNAME

in order to switch to your dbname.

Probably you are running that query on master database, so just do this and should work:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文