create function fun3(@m int=10,@n int=8)
獻(xiàn)縣網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),獻(xiàn)縣網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為獻(xiàn)縣上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的獻(xiàn)縣做網(wǎng)站的公司定做!
returns int
as begin
return @m+@n
end
go
select dbo.fun3(default,10);
--弟弟,變量聲明弄錯了,是@m int =10 而不是 @m=10 int
多寫。我剛接觸存儲過程的時候,感覺什么都不懂。慢慢接觸多了,寫的存儲過程也越來越難,現(xiàn)在就熟練了。
建議復(fù)習(xí):
建庫、建表,刪庫、刪表。
select/update/delete 的使用
通配符、運(yùn)算符、聚合函數(shù)
union/jion/group by/order by/having等的使用
子查詢
視圖、存儲過程、函數(shù)、觸發(fā)器的使用
另外掌握T-SQL的基本語法,包括if else/when case/with/while等等。
數(shù)據(jù)庫設(shè)計方面,E-R圖、范式的使用。
暫想了這么多。
還是先上代碼吧
,可以先看
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
@@error0
begin
rollback
tran
--出錯則回滾
end
else
begin
--否則提前事務(wù)
commit
tran
end
我的數(shù)據(jù)是這樣的:因為item_uid是標(biāo)識列,item_number有重復(fù)的,
我想過濾成這樣:
順帶說幾個在編程的時候遇到的小問題
1.程序
出現(xiàn)
Could
not
find
stored
procedure
找不到這個存儲過程
因為我的程序數(shù)據(jù)庫有四個,而默認(rèn)連接是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);
在這感謝
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.
不過我找到解決辦法,不過很麻煩,
(第二個回答用
'
sql
'進(jìn)行連接)
(用case
end
也行)
4.
select
into
和
insert
into
select
兩種復(fù)制文句
(這里感謝)
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不存在,因為在插入時會自動創(chuàng)建表Table2,并將Table1中指定字段數(shù)據(jù)復(fù)制到Table2中。
5.順便復(fù)習(xí)下常用的SQL方法語句
復(fù)制代碼
代碼如下:
declare
@name
varchar(200)
--聲明變量
set
@name='abcd;def'
--賦值
'exec
len
:'+Convert(varchar(10),Len(@name))
--convert(type,value)轉(zhuǎn)換,Len(value)獲取大小
'exec
charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)
在value中查找find的位置
'not
replace:'+@name
'exec
replace:'+Replace(@name,';','')
--用replace替換
'exec
substring:'+Substring(@name,0,3)--用substring截取
@@RowCount
--返回上一行代碼受影響的行數(shù)
作者:chenhuzi
《東南大學(xué)SQL數(shù)據(jù)庫基礎(chǔ)資料》百度網(wǎng)盤資源免費(fèi)下載
鏈接:
?pwd=d7k3 提取碼: d7k3
東南大學(xué) SQL數(shù)據(jù)庫基礎(chǔ) 全39講 主講-幸研 附復(fù)習(xí)課|各學(xué)科 學(xué)習(xí)視頻目錄|復(fù)習(xí)課|數(shù)據(jù)庫基礎(chǔ)003.csf|數(shù)據(jù)庫基礎(chǔ)002.csf|數(shù)據(jù)庫基礎(chǔ)001.csf|數(shù)據(jù)庫基礎(chǔ)(復(fù)習(xí))042.csf|數(shù)據(jù)庫基礎(chǔ)(復(fù)習(xí))041.csf|數(shù)據(jù)庫基礎(chǔ)(復(fù)習(xí))040.csf|政治目錄.txt|哲學(xué)目錄.txt|語言目錄.txt|藝術(shù)目錄.txt|醫(yī)學(xué)目錄.txt|心理目錄.txt
可以用最簡單的:SQLServer,MYSQL!裝好后,新建個數(shù)據(jù)庫,然后再建表,然后再造些數(shù)據(jù),最后在表里寫SQL語句,可以直接運(yùn)行的!
分享題目:sqlserver復(fù)習(xí),sqlserver試題
分享URL:http://muchs.cn/article4/hcgjoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、微信公眾號、、網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)