sqlserver中怎么利用存储过程去除重复行

31次阅读
没有评论

共计 2697 个字符,预计需要花费 7 分钟才能阅读完成。

这篇文章给大家介绍 sqlserver 中怎么利用存储过程去除重复行,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

代码如下:
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 – 返回上一行代码受影响的行数

关于 sqlserver 中怎么利用存储过程去除重复行就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-04发表,共计2697字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)