RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
sqlserver重复行,sql重复执行

sqlserver,在两条完全相同纪录,同时列当中包含有text这种不能被distinct和group的字段,应该怎么去除重复行

这个里面唯一的工作就是去重复,去重复的办法很多,关键的就是看效率问题咯,distinct, in, not in ,exists ,not exists.这些关键字,看楼主用的in,改用exists可能会提高一点效率,个人喜欢用exists,特别是大数据。小数据么,无所谓了。。呵呵。。因为在一定的情况下in会比exists执行效率高。建议工具里面增加对数据量大小的判断,然后再去组建不同的语句,可能会有很好的效果。对于in,exists在什么情况下效率的好坏,这个要测试滴。

成都创新互联服务项目包括晋中网站建设、晋中网站制作、晋中网页制作以及晋中网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,晋中网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到晋中省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

sqlserver 如何横向刷新重复数据

示例,创建数据表stuinfo,有三个字段recno(自增),stuid,stuname:

CREATE TABLE [StuInfo] ([recno] [int] IDENTITY (1, 1) NOT NULL ,[stuid] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,[stuname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL) ON [PRIMARY]GO

一、查某一列(或多列)的重复值。(只可以查出重复记录的值,不能查出整个记录的信息)

例如:查找stuid,stuname重复的记录:

select stuid,stuname from stuinfogroup by stuid,stunamehaving(count(*))1

二、查某一列有重复值的记录。(此方法查出的是所有重复的记录,如果有两条记录重复的,就查出两条)

例如:查找stuid重复的记录:

select * from stuinfowhere stuid in (select stuid from stuinfogroup by stuidhaving(count(*))1)

三、查某一列有重复值的记录。(只显示多余的记录,也就是说如果有三条记录重复的,就显示两条)

前提:需有一个不重复的列,此示例为recno。例如:查找stuid重复的记录:

select * from stuinfo s1where recno not in (select max(recno) from stuinfo s2where s1.stuid=s2.stuid

sqlserver利用存储过程去除重复行的sql语句

还是先上代码吧

,可以先看

SQL语句去掉重复记录,获取重复记录

复制代码

代码如下:

ALTER

procedure

[dbo].[PROC_ITEMMASTER_GETUNIQUE]

@PAGEINDEX

INT,@uid

int,@itemnumber

varchar(50)

AS

begin

tran

--开始事务

drop

table

[ItemMaster].[dbo].[testim]

--删除表

--把不重复记录转存到testim中

select

*

into

[ItemMaster].[dbo].[testim]

from

[ItemMaster].[dbo].[dat_item_master]

where

item_uid

in(select

min(item_uid)

as

item_uid

from

[ItemMaster].[dbo].[dat_item_master]

group

by

item_number)

and

status=0

select

top

10

*

from

[ItemMaster].[dbo].[testim]

where

item_uid

not

in

(select

top

(10*(@PAGEINDEX-1))

item_uid

from

[ItemMaster].[dbo].[testim])

and

owneruid=@uid

and

item_number

like

@itemnumber+'%'

--判断是否出错

if

@@error0

begin

rollback

tran

--出错则回滚

end

else

begin

--否则提前事务

commit

tran

end

我的数据是这样的:因为item_uid是标识列,item_number有重复的,

我想过滤成这样:

顺带说几个在编程的时候遇到的小问题

1.程序

出现

Could

not

find

stored

procedure

找不到这个存储过程

因为我的程序数据库有四个,而默认连接是A,但实际要执行B库里的存储过程,导致出错,

解决办法1:可在A里面建个一样的存储过程2:在执行连接的时候,替换下数据库就行了

2.

asp.net/C#

将存储过程中返回的数据集,填充到dataset/datatable

复制代码

代码如下:

SqlConnection

conn

=

new

SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());

SqlCommand

cmd

=

new

SqlCommand("Test",conn);

cmd.CommandType

=

CommandType.StoredProcedure;

cmd.Parameters.Add("@MaxId",

SqlDbType.Int).Value

=

12000;

SqlDataAdapter

sda

=

new

SqlDataAdapter(cmd);

DataTable

dt

=

new

DataTable();

sda.Fill(dt);

在这感谢

3.在存储过程里面,写SQL语句不能动态不加order

by

功能

比如

复制代码

代码如下:

--·@new_orderby

是传入参数,不能这样写

select

top

(10*(2-1))

item_uid

from

testim

order

by

@new_orderby

--执行这个的时候,SQL会出现

The

SELECT

item

identified

by

the

ORDER

BY

number

1

contains

a

variable

as

part

of

the

expression

identifying

a

column

position.

Variables

are

only

allowed

when

ordering

by

an

expression

referencing

a

column

name.

不过我找到解决办法,不过很麻烦,

(第二个回答用

'

sql

'进行连接)

(用case

end

也行)

4.

select

into

insert

into

select

两种复制文句

(这里感谢)

1.INSERT

INTO

SELECT语句

语句形式为:Insert

into

Table2(field1,field2,...)

select

value1,value2,...

from

Table1

要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

2.SELECT

INTO

FROM语句

语句形式为:SELECT

vale1,

value2

into

Table2

from

Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

5.顺便复习下常用的SQL方法语句

复制代码

代码如下:

declare

@name

varchar(200)

--声明变量

set

@name='abcd;def'

--赋值

print

'exec

len

:'+Convert(varchar(10),Len(@name))

--convert(type,value)转换,Len(value)获取大小

print

'exec

charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)

在value中查找find的位置

print

'not

replace:'+@name

print

'exec

replace:'+Replace(@name,';','')

--用replace替换

print

'exec

substring:'+Substring(@name,0,3)--用substring截取

print

@@RowCount

--返回上一行代码受影响的行数

作者:chenhuzi


名称栏目:sqlserver重复行,sql重复执行
URL链接:http://scyingshan.cn/article/hdijep.html