這篇文章給大家介紹sqlserver中怎么利用存儲(chǔ)過程去除重復(fù)行,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)建站專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、新邱網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為新邱等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
代碼如下: 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)識列,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復(fù)制代碼 代碼如下: 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 功能 比如復(fù)制代碼 代碼如下: --·@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方法語句復(fù)制代碼 代碼如下: 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ù)
關(guān)于sqlserver中怎么利用存儲(chǔ)過程去除重復(fù)行就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:sqlserver中怎么利用存儲(chǔ)過程去除重復(fù)行
本文URL:http://jinyejixie.com/article20/ghdsco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、虛擬主機(jī)、網(wǎng)站內(nèi)鏈、網(wǎng)頁設(shè)計(jì)公司、Google、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)