【发布时间】:2015-01-15 13:17:40
【问题描述】:
I am attempting to do the following
1. Link two tables via a join on the same database
2. Take a column that exists in both FK_APPLICATIONID(with a slight difference,
where one = +1 of the other I.e. Column 1 =1375 and column 2 = 1376
3. In one of the tables exist a reference number (QREF1234) and the other
contains 11 phonenumbers
4. I want to be able to enter the Reference number, and it returns all 11
phonenumbers as a single declarable value.
5. use "Select * from TableD where phonenum in (@Declared variable)
这是我目前所拥有的,
Use Database 1
DECLARE @Result INT;
SELECT @Result = D.PhoneNum1,
FROM Table1
JOIN TABLE2 D on D.FK_ApplicationID= D.FK_ApplicationID
where TABLE1.FK_ApplicationID = D.FK_ApplicationID + 1
and QREF = 'Q045569/2'
Use Database2
Select * from Table3 where PhoneNum = '@result'
我向以下不理解我想要达到的目标的人道歉,我希望这能解决问题。
谢谢
【问题讨论】:
-
你的
JOIN DB1.Table2 D on D.FK_ApplicationID= D.FK_ApplicationID是错误的。连接的两边是相同的。 -
您的代码毫无意义。它使用未定义的别名(例如
db1)和无意义的比较(例如D.FK_ApplicationID = D.FK_ApplicationID)。修复您的查询。此外,添加示例数据和所需结果。 -
为什么不在一个查询中引用表?语法类似于 Select * from MyDB1.dbo.TableA where PhoneNumber1 in (Select PhoneNumber1 from MyDB2.dbo.TableB)
-
所以您的表结构包含字段
Phonenum1、Phonenum2、Phonenum3、...、Phonenum11,并且您想将电话号码与这些字段中的任何一个匹配?跨度> -
我完全不明白你的最终目标声明。
标签: sql sql-server variables declare