sqlserver利用存储过程去除重复行的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);
在这感谢 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.
不过我找到解决办法,不过很麻烦,
(第二个回答用 ' sql '进行连接)
(用case end 也行)
4. select into 和 insert into select 两种复制文句 (这里感谢)
1.
语句形式为:
2.
语句形式为:
。
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 --返回上一行代码受影响的行数
作者:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t

MySQL methods for deleting stored procedures include using the DROP PROCEDURE statement, using MySQL Workbench, and using command line tools. Detailed introduction: 1. Use the DROP PROCEDURE statement. The steps are to first open the MySQL client or use any tool that supports MySQL, then connect to your MySQL database, and finally execute the following SQL statement to delete the stored procedure; 2. Use MySQL Workbench to delete Stored procedures and so on.

Implementation Principles and Applications of Golang Stored Procedures Stored procedures are precompiled programs that are stored in relational databases and can be called by applications. They can effectively reduce the cost of network transmission of data and improve the execution efficiency of the database. Although Golang does not directly support stored procedures, you can simulate the functions of stored procedures by using SQL statements. This article will introduce the principles and applications of implementing stored procedures in Golang, and provide specific code examples. 1. The implementation principle of Golang stored procedure is in Gol

Title: Detailed comparison and advantage analysis of Oracle stored procedures and functions. In Oracle database, stored procedures and functions are two important database objects. They can both be used to encapsulate a series of SQL statements and logic to improve the efficiency and complexity of data operations. Usability. This article will compare the characteristics of Oracle stored procedures and functions in detail, as well as their respective advantages, and provide specific code examples. Stored procedure A stored procedure is a set of SQL statements and PL/SQL code logic that are pre-written and stored in the database.

Performance Optimization Strategies for Batch Updates of Oracle Stored Procedures In Oracle database, a stored procedure is a database object used to process data logic or perform specific tasks. It can provide certain performance optimization strategies, especially when updating data in batches. Updating data in batches usually involves a large number of row-level operations. In order to improve performance and efficiency, we can adopt some strategies and techniques to optimize the performance of stored procedures. The following will introduce some performance optimization strategies for batch updates of Oracle stored procedures and provide specific code examples.

Golang is a powerful programming language that can easily implement stored procedures. In this article, we will introduce how to write efficient stored procedures using Golang and the benefits of using them in your projects.

How to write maintainable stored procedures in Golang In Golang, if you want to write maintainable stored procedures, you first need to understand the concept of stored procedures and how to implement them in Golang. A stored procedure is a reusable block of code stored in a database that contains a series of SQL statements. Stored procedures simplify code, improve performance, and encapsulate business logic. This article will introduce how to write maintainable stored procedures in Golang and provide specific code examples. 1. Connect to the database first
