sql 存储过程 去除重复行 sql 常用方法

以前弄过类似,去除相同信息的方法,现在找不到了,不过今天又花一些时间给弄出来了,记录一下,还是先上代码吧  这里感谢它 

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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有重复的,

             2010062513532546.png

           我想过滤成这样:

    2010062514041043.png

 

 

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

1.程序 出现  Could not find stored procedure 找不到这个存储过程

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

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

2. asp.net/C# 将存储过程中返回的数据集,填充到dataset/datatable 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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方法语句

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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 -- 返回上一行代码受影响的行数

 

 

 

 

 

欢迎补充哈

转载于:https://www.cnblogs.com/chenhuzi/archive/2010/06/25/1764979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值