将select语句传递给SQL Server函数

时间:2017-04-06 16:13:13

标签: sql-server tsql

我不知道这是否可行,但我想知道是否可以将SQL Server SELECT语句传递给SQL Server自定义函数。我需要能够将任何表中的数据与交叉进行比较,并返回具有相同数据的表。

但是,我需要执行此操作的表格将包含各种列。我需要比较20种不同的表格,我可以根据需要进行格式化。这有可能吗?是否可以将#tableVar抛出到函数或存储过程中?

以下是我现在正在进行的手动流程,为了安全起见,Select语句已略有修改:

Select * into #tableFirst FROM [dbo].~scrubed~
where insertdate >= '2016-09-05' and InsertDate < '2016-09-06';
Select * into #tableSecond FROM [dbo].~scrubed~
where insertdate >= '2016-09-06' and InsertDate < '2016-09-07';

select onion.* into #tableIntersect from (
Select * from #tableFirst 
intersect Select * from #tableSecond
) as onion;
--Return Data
Select 'Row Matching' as RowMatching, * from #tableIntersect

1 个答案:

答案 0 :(得分:1)

select count(*), a.column1, a.column2 from (
select * from [dbo].~scrubed~
union all
select * from [dbo].~scrubed~) a
group by a.column1, a.column2
having count(*) > 1

上述查询的结果已匹配。