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
--
*/
查看全文
相关阅读:
Porter Stemming Algorithm
Hook API to detect memory leak
Are tuples more efficient than lists in Python?
boost::intrusive_ptr VS boost::shared_ptr
How Python GC deal with referencecycles?
LINQ排序数组
Sublime Text 2 (Version 2.0.1 build 2217) x64 破解注册方法
GC in C# and Python
Python中的else
Managed C++ Destructor
原文地址:https://www.cnblogs.com/ghd258/p/260769.html
最新文章
lmth1 一个用Python编写的便捷网页信息提取工具
9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
TUP Masters系列第四期 搜索与云计算首席科学家Raghu Ramakrishnan:深入云计算实战 现场QA实录
Alpha、Beta、RC、GA版本的区别
查看修改oracle数据库字符集
关于ArcGIS Manager 登陆信息不正确原因
Installing CentOS 6 Device may need to be reinitialized
Linux下chkconfig命令详解
CentOS 6.3 tigerVNC 安装 (带字体错误问题)
给我们宿舍做的游戏snack
热门文章
mac m1 nvm 安装node版本失败
elementui eltabs 默认focus焦点框
mac m1 命令行报错 complete:13: command not found: compdef
ES5的继承与ES6的继承的区别——FEI面试官养成系列
vue2升级至vue3 项目技术栈升级攻略
如何写代码才显得没那么“菜”
heic图片的前端处理方式
ref and out in C++/CLI
JSON为何有正斜杠’/’转义?
How does Python's super() work with multiple inheritance?
Copyright © 2011-2022 走看看