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
--
*/
查看全文
相关阅读:
SQLite Java Wrapper/JDBC Driver(收集)
JAVA 并行运行(收集)
log4net使用方法(转载)
WMI服务故障,VBS脚本无法运行错误
ArcEngine中UID使用资料收集
使用 ArcGIS Engine Runtime 制作安装包(转载)
Eclipse安装WindowBuilder Pro(转载)
C#操作SQL Server数据库
自动化测试 (三) Web自动化测试原理
HTTP协议 (六) 状态码详解
原文地址:https://www.cnblogs.com/ghd258/p/260769.html
最新文章
mysql 导出数据到txt文件
读书笔记记录读书心得
建造者模式示例代码
Mysql的安全设置
mysql in 子查询 效率慢 优化(转)
正确实现 IDisposable 接口(转)
mysql 导入设置字符集
SpringBoot 配置文件application.properties使用详解(附:Profile多环境配置)
InfluxDB时序数据库的安装使用教程3(InfluxQL查询语言详解)
SpringBoot 配置文件application.yml使用详解(附:Profile多环境配置)
热门文章
SpringBoot 整合Thymeleaf模板引擎(附样例)
SpringBoot 使用Phoenix操作HBase教程3(使用MyBatis)
InfluxDB时序数据库的安装使用教程4(保留策略RP)
SpringBoot 路径映射(实现不通过Controller直接访问模版页面)
SpringBoot 自定义启动Banner(附:使用艺术字体)
SpringBoot 详细入门教程(创建、运行项目,REST服务样例)
SpringBoot 实现JSON数据的返回(将模型转成JSON字符串)
ARCGIS中坐标转换及地理坐标、投影坐标定义(转载)
.NET 多线程并行运算(转载)
用dom4j操作XML文档(收集)
Copyright © 2011-2022 走看看