zoukankan
html css js c++ java
查询重复记录
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 [
'
+
@tbname
+
'
] add [
'
+
@keyfdname
+
'
] decimal(38,0) identity(1,1)
'
)
end
select
@fd
=
''
,
@tj
=
''
select
@fd
=
@fd
+
'
,[
'
+
name
+
'
]
'
,
@tj
=
@tj
+
'
[
'
+
name
+
'
]=a.[
'
+
name
+
'
] and
'
from
syscolumns
where
object_name
(id)
=
@tbname
and
name
<>
@keyfdname
set
@fd
=
substring
(
@fd
,
2
,
8000
)
exec
(
'
select
'
+
@fd
+
'
from [
'
+
@tbname
+
'
] a
where exists(select 1 from [
'
+
@tbname
+
'
] where
'
+
@tj
+
'
[
'
+
@keyfdname
+
'
]<>a.[
'
+
@keyfdname
+
'
])
'
)
if
@nokey
=
1
exec
(
'
alter table [
'
+
@tbname
+
'
] drop column [
'
+
@keyfdname
+
'
]
'
)
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
--
*/
查看全文
相关阅读:
李洪强经典面试题27
李洪强经典面试题26(选择题)
java中InputStream String
[Android]Android5.0实现静默接听电话功能
Please verify that your device’s clock is properly set, and that your signing certificate is not exp
CentOS7虚拟机桥接设置及问题
GuozhongCrawler系列教程 (5) TransactionRequest具体解释
js之substr和substring的差别
hdu 3549 Flow Problem
JSP与HTML的差别
原文地址:https://www.cnblogs.com/ghd258/p/260769.html
最新文章
asp.net调用oracle存储过程
Lucene
(转)Silverlight_5_Toolkit_December_2011 安装后点击Toolkit Samples没反应的解决方法
open阶段的一致性检验(二)
华为測试 坐标移动
bootstrap之Orientation
Windows简单入门-送给第一次使用电脑的朋友
简单的横向ListView实现(version 3.0)
codecombat之边远地区的森林12-22关及地牢39关代码分享
桥接模式
热门文章
hdoj-1870-愚人节的礼物(栈)
jquary依据td中button的元素属性删除tr行(删选出想删除的行)
优雅的项目配置--经常使用库和版本号管理
java8_api_xml
李洪强iOS开发之断点续传1
李洪强iOS开发之OC点语法和变量作用域
李洪强iOS开发之OC面向对象—多态
李洪强iOS开发之【Objective-C】07-自定义构造方法和description方法
李洪强iOS开发之多线程编程2-NSOperation
iOS开发之都兴忱小结
Copyright © 2011-2022 走看看