想要在具有不同数据库的两个表中减去两列 [英] Want to subtract two columns in two tables with different databases

查看:122
本文介绍了想要在具有不同数据库的两个表中减去两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想从两个不同的数据库表中减去两列。例如



Hi Guys,

I want to subtract two columns from two different data base table. For example

DB1,TB1, Column 1
DB2, TB2, Column 2



想要减去第1列 - 第2列。我正在尝试这个



i我收到错误,你能帮我吗



什么我试过了:




want to subtract column 1 - column 2. I am trying this

i am getting an error, can you please help me

What I have tried:

Select tb1.column1, tb2.column1,
tb1.column1 - tb2.column1 AS calculation
FROM DB1.tb1 INNER JOIN DB2.TB2
ON tb1.column1 = tb2.column1

推荐答案

如果数据库位于同一个SQL Server实例上,但位于不同的数据库,你需要在引用表时使用三部分命名语法是

[数据库名称]。[架构]。[表名]

如果架构是dbo你不需要指定它。否则通常需要架构。



因此,架构是dbo,你的示例查询看起来像

If the databases are on the same SQL Server instance but on different databases, you need to use three part naming when referencing the table The syntax is
[database name].[schema].[table name]
If the schema is dbo you don't need to specify it. Otherwise schema is commonly needed.

So taken that the schema is dbo, your example query could look something like
SELECT t1.column1, t2.column1, t1.column1 - t2.column1 AS calculation
FROM          DB1..TB1 t1 
   INNER JOIN DB2..TB2 t2 ON t1.column1 = t2.column1


这篇关于想要在具有不同数据库的两个表中减去两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆