我在Access 2010中工作,并且遇到了糟糕的桌面设计。
该表有多列,可能包含我需要找到的名称。
即 公司,公司DBA1,公司DBA2,DBA3,DBA4,DBA5等...公司DBA12
我提出的最好的是:(假设我正在寻找jbl)
Select Company ,ClientNumber
From tblClient
Where Company = 'jbl' or DBA1='jbl' or DBA2='jbl'... DBA12='jbl'
虽然它有效但却非常慢!!!
我发现如果我能够使用SQLServer(我不能),我可以使用这样的存储过程:
BEGIN
SET NOCOUNT ON;
declare @Sql varchar(max)
set @sql =
'select Company ,ClientNumber
from dbo.tblClient
where isnull([Company],'''') + isnull( [Company DBA],'''') + isnull( [Company DBA2],'''') + isnull( [Company DBA3],'''') + isnull( [Company DBA4],'''') + isnull( [Company DBA5],'''') + isnull( [Company DBA6],'''') + isnull( [Company DBA7],'''') + isnull( [Company DBA8],'''') + isnull( [Company DBA9],'''') + isnull( [Company DBA10],'''') + isnull( [Company DBA11],'''') + isnull( [Company DBA12],'''') like ''%'+@Look4+'%''
order by company'
print @sql
exec (@sql)
END
这很快。
所以我想知道是否有必要在MS Access 2010中创建类似的查询或存储过程?
答案 0 :(得分:1)
最好使用联合查询来使表格看起来正常:
SELECT q.Company, q.ClientNumber, q.DBA
FROM
(SELECT Company, ClientNumber, DBA1 As DBA FROM tblClient
UNION All
SELECT Company, ClientNumber, DBA2 As DBA FROM tblClient
....) As q
Where q.Company = "jbl" or q.DBA="jbl"
当然,您可以将规范化版本保存为查询,然后在其他查询中使用该“视图”,这将为您提供:
PARAMETERS SearchString Text;
SELECT Company, ClientNumber, DBA
FROM ANormalTable
WHERE Company = SearchString Or DBA = SearchString
请注意,一旦添加没有明确包含null的WHERE,就会忽略所有空值。
答案 1 :(得分:1)
直接转换为Access将是:
select * from tblClient
where company & dba1 & dba2 & dba3 like '*jbl*'
编辑: 要进行完全匹配,您可以这样做:
select * from tblClient
where '|' & company & '|' & dba1 & '|' & dba2 & '|' & dba3 & '|' like '*|' & 'jbl' & '|*'