人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2022-01-28 18:43 | 文章來源:腳本之家

復(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ù)是這樣的:因?yàn)閕tem_uid是標(biāo)識列,item_number有重復(fù)的,

我想過濾成這樣:

順帶說幾個在編程的時候遇到的小問題 1.程序 出現(xiàn) Could not find stored procedure 找不到這個存儲過程 因?yàn)槲业某绦驍?shù)據(jù)庫有四個,而默認(rèn)連接是A,但實(shí)際要執(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 '進(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會自動創(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

香港穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實(shí)時開通

自選配置、實(shí)時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部