2017-08-08 76 views
1

我想與Company@MyList.CompanyNo來延長ListA,普萊舍參考代碼清單以結果從SQL/T-SQL子查詢到父select語句

數據&初始化:

begin /*Just the init data*/ 
DECLARE @MyList TABLE (Mail nvarchar(max), CompanyNo int) 
INSERT INTO @MyList VALUES ('...com',20) 
INSERT INTO @MyList VALUES ('...com',230) 
INSERT INTO @MyList VALUES ('...com',120) 
INSERT INTO @MyList VALUES ('...com',223) 
end 

--DECLARE 
DECLARE @ListA TABLE (Id nvarchar(max), Mail nvarchar(max)) 
DECLARE @ListB TABLE (Id nvarchar(max), Mail nvarchar(max),Company int) 

啓動點(此作品):

INSERT INTO @ListA(Id,Mail) select someId,name from [somedb].[dbo].aers where name IN (SELECT Mail FROM @MyList) 

我試圖按照以下方法:

INSERT INTO @ListB(Id,Mail,Company) select someId,name,@MyList.CompanyNo from [somedb].[dbo].aers where name IN (SELECT Mail FROM @MyList) 

所以實際上我想擴展ListB與相應的@MyList.CompanyNo

謝謝,我該怎麼辦?

回答

0

你可以根據病情使用JOINWHERE

INSERT INTO @ListB(Id,Mail,Company) 
select a.someId,a.name,m.CompanyNo 
from [somedb].[dbo].aers a 
join @MyList m 
    ON a.name = m.Mail;