国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

查詢重復記錄

2024-07-21 02:08:04
字體:
來源:轉載
供稿:網友

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_qry]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[p_qry]
go

/*--查詢重復記錄的通用存儲過程
 
 可以查詢出表中那些數據是重復的,這里的重復,是指除主鍵外重復的記錄
 如果表中有主鍵,請指定主鍵.
 如果表中有標識字段,而且標識字段無重復,請在調用時,將主鍵指定為標識字段
  如果標識字段重復,不能用此存儲過程

-- 2004.4--*/

create proc p_qry
@tbname sysname,  --要查詢的表名
@keyfdname sysname=null --表中的主鍵,如果未指定,則表中無主鍵
as
declare @nokey bit,@fd varchar(8000),@tj varchar(8000)
set nocount on
if isnull(@keyfdname,'')=''
begin
 select @keyfdname=cast(newid() as char(36)),@nokey=1
 exec('alter table ['[email protected]+'] add ['+[email protected]+'] decimal(38,0) identity(1,1)')
end
select @fd='',@tj=''
select @[email protected]+',['+name+']'
 ,@[email protected]+'['+name+']=a.['+name+'] and '
from syscolumns
where object_name(id)[email protected] and name<>@keyfdname
set @fd=substring(@fd,2,8000)
exec('select '[email protected]+' from ['[email protected]+'] a
 where exists(select 1 from ['[email protected]
 +'] where a.['[email protected]+'])'">'[email protected]+'['[email protected]+']<>a.['[email protected]+'])')
if @nokey=1
 exec('alter table ['[email protected]+'] drop column ['[email protected]+']')
set nocount off
go

--調用示例
--創建測試數據
create table 表(f1 int,f2 int,f3 int,f4 int,f5 int)
insert into 表
select 1,1,1,1,1
union all select 2,1,1,1,1
union all select 3,2,1,23,1
union all select 4,2,3,1,3
union all select 5,1,1,1,1
go

--調用通用存儲過程實現樓主的查詢
exec p_qry '表','f1'

--刪除測試環境
drop table 表


/*--測試結果

f2          f3          f4          f5         
----------- ----------- ----------- -----------
1           1           1           1
1           1           1           1
1           1           1           1
--*/

 
 



收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 民县| 遂平县| 广宁县| 玉田县| 黑河市| 沙田区| 永寿县| 婺源县| 星子县| 漯河市| 泰顺县| 汝南县| 和硕县| 清水县| 黄陵县| 辽宁省| 深水埗区| 通河县| 新余市| 墨江| 沭阳县| 芮城县| 潞城市| 长顺县| 齐齐哈尔市| 宁陵县| 定结县| 富裕县| 务川| 张北县| 永清县| 兴和县| 泰安市| 广州市| 长宁县| 新郑市| 奉节县| 肥西县| 南岸区| 富川| 台江县|