问题
I am attempting to do the following
- Link two tables via a join on the same database
- 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
- In one of the tables exist a reference number (QREF1234) and the other contains 11 phonenumbers
- I want to be able to enter the Reference number, and it returns all 11 phonenumbers as a single declarable value.
- use
Select * from TableD where phonenum in (@Declared variable)
Here is what I have so far,
Use Database 1
DECLARE @Result INT;
SELECT @Result = D.PhoneNum1,phonenum2,phonenum3,etc
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'
The names of things like TABLE1 is not their true name
Thanks
回答1:
I think you are after something like this. You are trying to "normalize" un-normalized columns and search for all those values in another table. You need to union the results together into a temp table, then search for the values.
Use Database 1
Create Table #tmp(PhoneNums varchar(50))
INSERT INTO #tmp
SELECT 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'
union
SELECT D.PhoneNum2
FROM Table1
JOIN TABLE2 D on D.FK_ApplicationID= D.FK_ApplicationID
where TABLE1.FK_ApplicationID = D.FK_ApplicationID + 1
and QREF = 'Q045569/2'
union
SELECT D.PhoneNum3
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
--you don't need to switch databases if you use a fully qualified name like shown below.
Select * from Database2..Table3 where PhoneNum in
(
Select PhoneNums from #tmp
)
回答2:
No, you cannot use a variable to hold multiple values and use the IN
keyword. You can pass a delimited list of values in, parse that, and then use IN
with the collection of values you parsed.
In the code sample you posted, you can instead create a table variable, and select from that.
回答3:
A variable can only hold a single value. Instead of using the in
operator, you could accompish this same thing with a join...
Use Database1
SELECT distinct
T3.*
FROM
Table1
JOIN TABLE2 D on D.FK_ApplicationID= D.FK_ApplicationID
JOIN Database2.dbo.Table3 T3 on
T3.PhoneNum = D.PhoneNum1
or T3.PhoneNum = D.PhoneNum2
or T3.PhoneNum = D.PhoneNum3
where
TABLE1.FK_ApplicationID = D.FK_ApplicationID + 1
and QREF = 'Q045569/2'
回答4:
If you don't want to type a bunch of ORs on a join you could use IN for your join.
select *
from Table3 t
left join Table2 D on t.PhoneNum in
(
d.phonenum1
, d.phonenum2
, d.phonenum3
, d.phonenum4
, d.phonenum5
, d.phonenum6
, d.phonenum7
, d.phonenum8
, d.phonenum9
, d.phonenum10
, d.phonenum11
)
AND D.QREF = 'Q045569/2'
I would recommend instead that you normalize your data and then this isn't such a kludge.
来源:https://stackoverflow.com/questions/27966404/sql-how-to-make-multiple-values-use-the-same-declare-variable