这是我将如何做到的。计划是获取每个结果集,然后将其转换为 XML 并比较两个 XML 结果。容易得多(在我看来)。
CREATE TABLE MyTable
(
[id] integer identity,
[Title] varchar(1024),
[Point] int,
[Date] datetime
);
insert into MyTable([Title], [Point], [Date])
values('T1', 1, '2012-04-26 07:14:34.000'),
('T1', 2, '2012-07-26 07:14:34.000'),
('T1', 3, '2012-06-26 07:14:34.000'),
('T1', 4, '2012-05-26 07:14:34.000'),
('T2', 1, '2012-04-26 07:14:34.000'),
('T2', 2, '2012-07-26 07:14:34.000'),
('T2', 3, '2012-06-26 07:14:34.000'),
('T2', 4, '2012-05-26 07:14:34.000'),
('T3', 4, '2012-05-26 07:14:34.000'),
('T3', 3, '2012-06-26 07:14:34.000'),
('T4', 1, '2012-04-26 07:14:34.000'),
('T4', 2, '2012-07-26 07:14:34.000'),
('T4', 3, '2012-06-26 07:14:34.000'),
('T4', 4, '2012-05-27 07:14:34.000'),
('T5', 2, '2012-12-27 07:14:34.000'),
('T5', 6, '2012-05-27 07:14:34.000'),
('T5', 3, '2012-07-26 07:14:34.000');
-- your original queries
Select Point, Date From MyTable Where Title = 'T1';
Select Point, Date From MyTable Where Title = 'T2';
-- first I am going to just get each one as XML
SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('');
SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T2'
FOR XML PATH('');
-- first just get the two results into one result set
select t.FirstCheck, t.SecondCheck
from (
select (SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('')) as FirstCheck,
(SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T2'
FOR XML PATH('')) as SecondCheck
) as t;
-- now for the real check.
declare @flag int;
select @flag = case when t.FirstCheck = t.SecondCheck then 1 else 0 end
from (
select (SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('')) as FirstCheck,
(SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T2'
FOR XML PATH('')) as SecondCheck
) as t;
-- this should return 1
select @flag as Flag;
select @flag = case when t.FirstCheck = t.SecondCheck then 1 else 0 end
from (
select (SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('')) as FirstCheck,
(SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T3'
FOR XML PATH('')) as SecondCheck
) as t;
-- this should return 0
select @flag as Flag;
如果我了解您要做什么,那么应该这样做。