濮阳杆衣贸易有限公司

主頁 > 知識庫 > sqlserver利用存儲過程去除重復(fù)行的sql語句

sqlserver利用存儲過程去除重復(fù)行的sql語句

熱門標簽:貴州房產(chǎn)智能外呼系統(tǒng)供應(yīng)商 電銷外呼有錄音系統(tǒng)有哪些 百度地圖標注改顏色 申請400電話在哪辦理流程 一個導(dǎo)航軟件能用幾個地圖標注點 小e電話機器人 鎮(zhèn)江網(wǎng)路外呼系統(tǒng)供應(yīng)商 外呼運營商線路收費 臨沂智能電銷機器人加盟哪家好
還是先上代碼吧 ,可以先看 SQL語句去掉重復(fù)記錄,獲取重復(fù)記錄
復(fù)制代碼 代碼如下:

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+'%'

--判斷是否出錯
if @@error>0
begin
rollback tran --出錯則回滾
end
else
begin --否則提前事務(wù)
commit tran
end

我的數(shù)據(jù)是這樣的:因為item_uid是標識列,item_number有重復(fù)的,

我想過濾成這樣:

順帶說幾個在編程的時候遇到的小問題

1.程序 出現(xiàn) Could not find stored procedure 找不到這個存儲過程

因為我的程序數(shù)據(jù)庫有四個,而默認連接是A,但實際要執(zhí)行B庫里的存儲過程,導(dǎo)致出錯,

解決辦法1:可在A里面建個一樣的存儲過程2:在執(zhí)行連接的時候,替換下數(shù)據(jù)庫就行了

2. asp.net/C# 將存儲過程中返回的數(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.在存儲過程里面,寫SQL語句不能動態(tài)不加order by 功能

比如

復(fù)制代碼 代碼如下:

--·@new_orderby 是傳入?yún)?shù),不能這樣寫
select top (10*(2-1)) item_uid from testim order by @new_orderby


--執(zhí)行這個的時候,SQL會出現(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   (第二個回答用 ' 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 兩種復(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

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

   2.SELECT INTO FROM語句

      語句形式為:SELECT vale1, value2 into Table2 from Table1

       要求目標表Table2不存在,因為在插入時會自動創(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ù)

作者:chenhuzi

您可能感興趣的文章:
  • 解析mysql中:單表distinct、多表group by查詢?nèi)コ貜?fù)記錄
  • sqlserver 用戶權(quán)限管理,LINQ去除它的重復(fù)菜單項
  • mysql SELECT語句去除某個字段的重復(fù)信息
  • Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù)
  • MySQL中distinct語句去查詢重復(fù)記錄及相關(guān)的性能討論
  • SQL高級應(yīng)用之同服務(wù)器上復(fù)制表到另一數(shù)據(jù)庫中并實現(xiàn)去重復(fù)
  • SQL分組排序去重復(fù)的小實例
  • oracle sql 去重復(fù)記錄不用distinct如何實現(xiàn)
  • SQL語句去掉重復(fù)記錄,獲取重復(fù)記錄
  • SQL去除重復(fù)記錄(七種)

標簽:澳門 日照 晉城 延邊 三明 合肥 保定 嘉興

巨人網(wǎng)絡(luò)通訊聲明:本文標題《sqlserver利用存儲過程去除重復(fù)行的sql語句》,本文關(guān)鍵詞  sqlserver,利用,存儲,過程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《sqlserver利用存儲過程去除重復(fù)行的sql語句》相關(guān)的同類信息!
  • 本頁收集關(guān)于sqlserver利用存儲過程去除重復(fù)行的sql語句的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    凉城县| 延边| 德令哈市| 政和县| 新建县| 广灵县| 石阡县| 古田县| 民乐县| 黎平县| 梁山县| 莫力| 九台市| 栾城县| 大荔县| 兴化市| 罗定市| 永登县| 喀喇沁旗| 宁乡县| 峨边| 海门市| 新野县| 新郑市| 濮阳市| 大丰市| 突泉县| 通道| 广南县| 乐山市| 保德县| 通渭县| 宁陵县| 封开县| 壶关县| 河池市| 安丘市| 类乌齐县| 津南区| 奉新县| 中牟县|