sqlserver利用存储过程去除重复行的sql语句
发布时间:2024-01-20 06:28:54
还是先上代码吧 ,可以先看 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 @@error<>0
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);
在这感谢 http://www.cnblogs.com/liujuncm5/archive/2009/08/31/1557569.html
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.
不过我找到解决办法,不过很麻烦,
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9328 (第二个回答用 ' sql '进行连接)
http://databases.aspfaq.com/database/how-do-i-use-a-variable-in-an-order-by-clause.html (用case end 也行)
4. select into 和 insert into select 两种复制文句 (这里感谢http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html)
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
猜你喜欢
- python数组添加整行或整列代码如下,见注释:import numpy as npm_list=[[1,2,3],[3,4,5]]m_ar
- python生成遍历暴力破解密码(这里已遍历暴力破解rar为例,只提供生成密码以及遍历密码)这个也就是提供一个思路,需求是这样的,我XX的闺
- 内容摘要: 当用户填写页面<FORM>内容时所提供的全部值,或在浏览器地址栏输入在URL后的值,通过Form和QueryStrin
- 装了 Access 2003 安全更新 (KB981716) 之后 Access 打不开,office2003-KB981716-FullF
- 第一种import win32clipboardimport time#速度快 容易出错class niubi(): def lihai(s
- 先了解如何利用python语言实现以平面和标记物进行姿态估计本实验只是先实现一个简单的小例子。简单来说就是先识别出图像中的参考面,再拍摄一张
- 在刚过去不久的6月30日那天,国外一位在YouTube拥有上千万粉丝的我的世界游戏主播Technoblade因癌症与世长辞,年仅23岁,他并
- 1.直方图直方图: (1) 图像中不同像素等级出现的次数 (2) 图像中具有不同等级的像素关于总像素数目的比值。我们使用cv2.calcHi
- 这篇文章主要介绍了python如何实现不可变字典inmutabledict,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 如果你的电脑内存较小那么想在本地做一些事情是很有局限性的(哭丧脸),比如想拿一个kaggle上面的竞赛来练练手,你会发现多数训练数据集都是大
- 1.散点图代码# This import registers the 3D projection, but is otherwise unu
- Python sorted() 函数sorted() 函数对所有可迭代的对象进行排序操作sorted 语法:sorted(iterable,
- 目录1、字典的定义字典和列表的区别:字典的基本使用2、循环遍历3、字符串的定义4、字符串的常用操作字符串 查找和替换字符串 文本对齐演练去除
- 目录1、吃金币2、打乒乓3、滑雪4、并夕夕版飞机大战5、打地鼠6、小恐龙7、消消乐8、俄罗斯方块9、贪吃蛇10、24点小游戏11、平衡木12
- 本文实例讲述了python获取本机mac地址和ip地址的方法。分享给大家供大家参考。具体如下:import sys, socketdef g
- 具体代码如下所述:import sysfrom PySide2.QtGui import *from PySide2.QtCore impo
- 使用T_SQL创建数据库 TestSchool 创建一个学生表 TblStudent 创建学生成绩表 TblScore q tScoreId
- Timer继承子Thread类,是Thread的子类,也是线程类,具有线程的能力和特征。这个类用来定义多久执行一个函数。它的实例是能够延迟执
- 场景分析一般Linux系统默认自带两个版本的python,我按照的虚拟机系统自带的版本为python2.7和python3.2,但是由于个人
- 约定:import pandas as pdimport numpy as npReIndex重新索引reindex()是pandas对象的