溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

sqlserver中怎么利用存儲(chǔ)過(guò)程去除重復(fù)行

發(fā)布時(shí)間:2021-08-02 16:08:39 來(lái)源:億速云 閱讀:143 作者:Leah 欄目:數(shù)據(jù)庫(kù)

這篇文章給大家介紹sqlserver中怎么利用存儲(chǔ)過(guò)程去除重復(fù)行,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

代碼如下: ALTER procedure [dbo].[PROC_ITEMMASTER_GETUNIQUE] @PAGEINDEX INT,@uid int,@itemnumber varchar(50) AS begin tran --開(kāi)始事務(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ù)的, 我想過(guò)濾成這樣: 順帶說(shuō)幾個(gè)在編程的時(shí)候遇到的小問(wèn)題 1.程序 出現(xiàn) Could not find stored procedure 找不到這個(gè)存儲(chǔ)過(guò)程 因?yàn)槲业某绦驍?shù)據(jù)庫(kù)有四個(gè),而默認(rèn)連接是A,但實(shí)際要執(zhí)行B庫(kù)里的存儲(chǔ)過(guò)程,導(dǎo)致出錯(cuò), 解決辦法1:可在A里面建個(gè)一樣的存儲(chǔ)過(guò)程2:在執(zhí)行連接的時(shí)候,替換下數(shù)據(jù)庫(kù)就行了 2. asp.net/C# 將存儲(chǔ)過(guò)程中返回的數(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ǔ)過(guò)程里面,寫(xiě)SQL語(yǔ)句不能動(dòng)態(tài)不加order by 功能 比如復(fù)制代碼 代碼如下: --·@new_orderby 是傳入?yún)?shù),不能這樣寫(xiě) 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.

不過(guò)我找到解決辦法,不過(guò)很麻煩,

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語(yǔ)句

語(yǔ)句形式為:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

要求目標(biāo)表Table2必須存在,由于目標(biāo)表Table2已經(jīng)存在,所以我們除了插入源表Table1的字段外,還可以插入常量。

  2.SELECT INTO FROM語(yǔ)句

語(yǔ)句形式為: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方法語(yǔ)句復(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ǔ)過(guò)程去除重復(fù)行就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI