我已经阅读了所有关于我无法找到的无关联表的帖子,但似乎没有任何东西可以帮助我(我能理解)。 我有2个临时表,我需要表2只包含行 text字段包含table1中的任何一个名称。两个表之间没有直接联系。
表1示例:
的 table1.fullname
一个名字
b名称
c名称
d名称
'主题的提取物'表2中的列:
的主题
巴哈马食品出口市场研究报告
初始电话会员
名称 - 更新电话以检查进度
更新电话以检查进度名称
b名字 - 与安娜见面
建议彼得 - b名字
c name-协助完成资金申请。
d name - Advice
我需要的只是表2中包含主题字段table1中任何名称的行。
我使用过类似的东西:
WHERE table2.subject LIKE '%'+table1.fullname+'%'
但它不会起作用,因为我没有以任何方式加入表格。
大多数帖子似乎建议使用交叉连接,但由于两个表都非常大,我不认为这是最好的方式。
我认为我应该使用子查询,但无法解决如何加入它。
到目前为止我的代码:
IF OBJECT_ID('tempdb.dbo.#ITA', 'U') IS NOT NULL
DROP TABLE #ITA SELECT *
INTO #ITA
FROM (select distinct fullname
from FilteredContact
Where (jobtitle LIKE 'International Trade Adviser%'))InternationalTradeAdviser;
IF OBJECT_ID('tempdb.dbo.#ITAactivity', 'U') IS NOT NULL
DROP TABLE #ITAactivity SELECT *
INTO #ITAactivity
FROM (SELECT distinct FilteredAppointment.activityid as ActivityID, Filteredccx_project.ccx_customerid AS CompanyID, Filteredccx_project.ccx_projectnumber AS ProjectNumber,
CONVERT(varchar, FilteredAppointment.scheduledstart, 103) AS ConvertedTime,
FilteredAppointment.actualdurationminutes, FilteredAppointment.actualdurationminutes / 60 AS Hours, FilteredAppointment.actualdurationminutes % 60 AS Minutes,
'International Trade Adviser' As TypeOfService
FROM FilteredAppointment INNER JOIN
Filteredccx_project ON FilteredAppointment.regardingobjectid = Filteredccx_project.ccx_projectid
WHERE (FilteredAppointment.statecode = 1) AND (FilteredAppointment.subject LIKE '%'+#ITA.fullname+'%') AND
(Filteredccx_project.ccx_programme IN ('04D0B1DF-FD36-E611-8870-005056A52209','14286237-FE36-E611-8870-005056A52209')) AND
Filteredccx_project.ccx_projectstatus IN(803080000,803080001) AND
Filteredccx_Project.ccx_runningtotal>=12)ITAactivities;
我尝试在WHERE子句中使用子查询但产生错误消息,因为它返回的行数超过1行。
答案 0 :(得分:1)
由于您说您只想要包含全名的记录,因此您也可以在exists
- 子句中使用where
。
为此,请更换部件
AND (FilteredAppointment.subject LIKE '%'+#ITA.fullname+'%')
用这个
AND exists (select 1
from #ITA it
where FilteredAppointment.subject like '%' + it.fullname + '%')
最终的插入看起来像这样:
SELECT *
INTO #ITAactivity
FROM (SELECT distinct
FilteredAppointment.activityid as ActivityID,
Filteredccx_project.ccx_customerid AS CompanyID,
Filteredccx_project.ccx_projectnumber AS ProjectNumber,
CONVERT(varchar, FilteredAppointment.scheduledstart, 103) AS ConvertedTime,
FilteredAppointment.actualdurationminutes, FilteredAppointment.actualdurationminutes / 60 AS Hours, FilteredAppointment.actualdurationminutes % 60 AS Minutes,
'International Trade Adviser' As TypeOfService
FROM FilteredAppointment
INNER JOIN
Filteredccx_project ON FilteredAppointment.regardingobjectid = Filteredccx_project.ccx_projectid
WHERE (FilteredAppointment.statecode = 1)
AND (Filteredccx_project.ccx_programme IN ('04D0B1DF-FD36-E611-8870-005056A52209', '14286237-FE36-E611-8870-005056A52209'))
AND Filteredccx_project.ccx_projectstatus IN (803080000, 803080001)
AND Filteredccx_Project.ccx_runningtotal >= 12
AND exists (select 1
from #ITA it
where FilteredAppointment.subject like '%' + it.fullname + '%'))ITAactivities;
答案 1 :(得分:0)
这种动态技术可能很有用:
Declare @FullNames as nvarchar(max)
Declare @Sql as nvarchar(max)
select @FullNames =
stuff((
select distinct ' or subject like ''%' + [fullname] + '%'''
from t1
for xml path('')
),1,3,'') as x
set @Sql = 'SELECT *
INTO #ITAactivity
FROM (SELECT distinct FilteredAppointment.activityid as ActivityID, Filteredccx_project.ccx_customerid AS CompanyID, Filteredccx_project.ccx_projectnumber AS ProjectNumber,
CONVERT(varchar, FilteredAppointment.scheduledstart, 103) AS ConvertedTime,
FilteredAppointment.actualdurationminutes, FilteredAppointment.actualdurationminutes / 60 AS Hours, FilteredAppointment.actualdurationminutes % 60 AS Minutes,
''International Trade Adviser'' As TypeOfService
FROM FilteredAppointment INNER JOIN
Filteredccx_project ON FilteredAppointment.regardingobjectid = Filteredccx_project.ccx_projectid
WHERE (FilteredAppointment.statecode = 1) AND (FilteredAppointment.subject LIKE ''%''+#ITA.fullname+''%'') AND
(Filteredccx_project.ccx_programme IN (''04D0B1DF-FD36-E611-8870-005056A52209'',''14286237-FE36-E611-8870-005056A52209'')) AND
Filteredccx_project.ccx_projectstatus IN(803080000,803080001) AND
Filteredccx_Project.ccx_runningtotal>=12)ITAactivities AND (' + @FullNames + ')'
exec(@Sql)