亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2010年06月29日 17:59:49   作者:  
以前弄過類似,去除相同信息的方法,現(xiàn)在找不到了,不過今天又花一些時(shí)間給弄出來了,記錄一下
還是先上代碼吧 ,可以先看 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+'%'

--判斷是否出錯(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


復(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ù)

作者:chenhuzi

相關(guān)文章

  • sqlserverdriver配置方法 jdbc連接sqlserver

    sqlserverdriver配置方法 jdbc連接sqlserver

    這篇文章主要介紹了sqlserverdriver配置方法,介紹了連接SqlServer2000 和連接SqlServer2005的方法,大家參考使用吧
    2014-01-01
  • SQL Server數(shù)據(jù)庫按百分比查詢出表中的記錄數(shù)

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

    這篇文章主要介紹了SQL Server數(shù)據(jù)庫在一個(gè)表中按百分比查詢出記錄條數(shù)的方法及代碼示例,需要的朋友可以參考下
    2015-08-08
  • SQL Server誤區(qū)30日談 第4天 DDL觸發(fā)器就是INSTEAD OF觸發(fā)器

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

    DDL觸發(fā)器的實(shí)現(xiàn)原理其實(shí)就是一個(gè)AFTER觸發(fā)器。這個(gè)意思是先發(fā)生DDL操作,然后觸發(fā)器再捕捉操作(當(dāng)然如果你在觸發(fā)器內(nèi)寫了Rollback,則也可能回滾)
    2013-01-01
  • sqlserver禁止management studio的自動(dòng)提交事務(wù)

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

    默認(rèn)management studio是自動(dòng)提交事務(wù),即一個(gè)語句就一個(gè)事務(wù),那么如何禁止其自動(dòng)提交呢?下面有個(gè)不錯(cuò)的方法,大家可以參考下
    2014-06-06
  • 最新評(píng)論