sqlserver利用存儲(chǔ)過程去除重復(fù)行的sql語句
ALTER procedure [dbo].[PROC_ITEMMASTER_GETUNIQUE] @PAGEINDEX INT,@uid int,@itemnumber varchar(50)
AS
begin tran --開始事務(wù)
drop table [ItemMaster].[dbo].[testim] --刪除表
--把不重復(fù)記錄轉(zhuǎn)存到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+'%'
--判斷是否出錯(cuò)
if @@error<>0
begin
rollback tran --出錯(cuò)則回滾
end
else
begin --否則提前事務(wù)
commit tran
end
我的數(shù)據(jù)是這樣的:因?yàn)閕tem_uid是標(biāo)識(shí)列,item_number有重復(fù)的,

我想過濾成這樣:

順帶說幾個(gè)在編程的時(shí)候遇到的小問題
1.程序 出現(xiàn) Could not find stored procedure 找不到這個(gè)存儲(chǔ)過程
因?yàn)槲业某绦驍?shù)據(jù)庫有四個(gè),而默認(rèn)連接是A,但實(shí)際要執(zhí)行B庫里的存儲(chǔ)過程,導(dǎo)致出錯(cuò),
解決辦法1:可在A里面建個(gè)一樣的存儲(chǔ)過程2:在執(zhí)行連接的時(shí)候,替換下數(shù)據(jù)庫就行了
2. asp.net/C# 將存儲(chǔ)過程中返回的數(shù)據(jù)集,填充到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.在存儲(chǔ)過程里面,寫SQL語句不能動(dòng)態(tài)不加order by 功能
比如
--·@new_orderby 是傳入?yún)?shù),不能這樣寫
select top (10*(2-1)) item_uid from testim order by @new_orderby
--執(zhí)行這個(gè)的時(shí)候,SQL會(huì)出現(xiàn) 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 (第二個(gè)回答用 ' sql '進(jìn)行連接)
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 兩種復(fù)制文句 (這里感謝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
要求目標(biāo)表Table2必須存在,由于目標(biāo)表Table2已經(jīng)存在,所以我們除了插入源表Table1的字段外,還可以插入常量。
2.SELECT INTO FROM語句
語句形式為:SELECT vale1, value2 into Table2 from Table1
要求目標(biāo)表Table2不存在,因?yàn)樵诓迦霑r(shí)會(huì)自動(dòng)創(chuàng)建表Table2,并將Table1中指定字段數(shù)據(jù)復(fù)制到Table2中。
5.順便復(fù)習(xí)下常用的SQL方法語句
declare @name varchar(200) --聲明變量
set @name='abcd;def' --賦值
print 'exec len :'+Convert(varchar(10),Len(@name)) --convert(type,value)轉(zhuǎn)換,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 --返回上一行代碼受影響的行數(shù)
作者:chenhuzi
- 解析mysql中:單表distinct、多表group by查詢?nèi)コ貜?fù)記錄
- sqlserver 用戶權(quán)限管理,LINQ去除它的重復(fù)菜單項(xiàng)
- mysql SELECT語句去除某個(gè)字段的重復(fù)信息
- Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù)
- MySQL中distinct語句去查詢重復(fù)記錄及相關(guān)的性能討論
- SQL高級(jí)應(yīng)用之同服務(wù)器上復(fù)制表到另一數(shù)據(jù)庫中并實(shí)現(xiàn)去重復(fù)
- SQL分組排序去重復(fù)的小實(shí)例
- oracle sql 去重復(fù)記錄不用distinct如何實(shí)現(xiàn)
- SQL語句去掉重復(fù)記錄,獲取重復(fù)記錄
- SQL去除重復(fù)記錄(七種)
相關(guān)文章
SQL中的開窗函數(shù)詳解可代替聚合函數(shù)使用
這篇文章主要介紹了SQL中的開窗函數(shù)詳解可代替聚合函數(shù)使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03SQLServer 數(shù)據(jù)庫開發(fā)頂級(jí)技巧
無論你的專業(yè)水平如何,從其他IT專家那里學(xué)習(xí)新的技巧與最佳實(shí)踐常常都是有益的。本文包含了我遇到過的SQL Server開發(fā)的高級(jí)技巧。希望其中的一些技巧能夠?qū)δ臄?shù)據(jù)庫開發(fā)及管理工作有所幫助。2009-07-07Sql Server事務(wù)語法及使用方法實(shí)例分析
這篇文章主要介紹了Sql Server事務(wù)語法及使用方法,結(jié)合實(shí)例形式分析了Sql Server事務(wù)的概念、原理及相關(guān)使用技巧,需要的朋友可以參考下2019-02-02修改SQL-SERVER數(shù)據(jù)庫表結(jié)構(gòu)的SQL命令附sql命令行修改數(shù)據(jù)庫
本教程給大家介紹修改SQL-SERVER數(shù)據(jù)庫表結(jié)構(gòu)的SQL命令附sql命令行修改數(shù)據(jù)庫,涉及到sqlserver數(shù)據(jù)庫命令的相關(guān)知識(shí),對(duì)sqlserver數(shù)據(jù)庫命令感興趣的朋友可以參考下本篇文章2015-10-10一個(gè)過濾重復(fù)數(shù)據(jù)的 SQL 語句
一個(gè)過濾重復(fù)數(shù)據(jù)的 SQL 語句...2006-12-12

sqlserverdriver配置方法 jdbc連接sqlserver

SQL Server數(shù)據(jù)庫按百分比查詢出表中的記錄數(shù)

SQL Server誤區(qū)30日談 第4天 DDL觸發(fā)器就是INSTEAD OF觸發(fā)器

sqlserver禁止management studio的自動(dòng)提交事務(wù)