sqlserver存儲過程寫法
直接在存儲過程結束之前添加以下代碼:
EXCEPTION WHEN OTHERS THEN rollback;
解釋:以上語句的意思是當出現任何錯誤的時候,直接回滾整個事務。
備註:存儲過程中建議沒有特殊需求的情況下,在執行結束之前不要進行commit,否則在中途提交,後面的代碼保存,只能回滾到commit的位置。
Ⅱ SqlServer存儲過程
create procere prCreateSubPlan
as
begin
declare @id int,
@intCycle int,
@planName varchar(100),
@createTime smalldatetime,
@cycleTime int
select @id = min(t_cplan_id) from t_cplan
while (@id is not null)
begin
select @planName=t_plan_name, @createTime = createTime, @cycleTime = cycleTime from t_cplan where t_cplan_id=@id
select @intCycle= 0
while (@intCycle<@cycleTime)
begin
-- 表t_plan 列t_plan_id是IDENTITY 列
insert t_plan (t_plan_name, t_cplan_id, createTime)
values (@planName, @id, dateadd(day, @intCycle, @createTime))
select @intCycle = @intCycle + 1
end
select @id = min(t_cplan_id) from t_cplan where t_cplan_id>@id
end
end
go
Ⅲ sqlserver怎麼創建存儲過程
1、可視化創建
a.登錄SQL Server
b.打開資料庫==》要創建存儲過程的資料庫==》可編程性==》存儲過程
c.選中「存儲過程」右擊 ,在系出現的對話框中選擇「新建存儲過程」
d.在右側出現的對話框中填寫具體存儲過程內容完成後執行即可
2、代碼創建
a.全手寫代碼
一、定義變數
--簡單賦值
declare@aint
set@a=5
print@a
--使用select語句賦值
declare@user1nvarchar(50)
select@user1='張三'
print@user1
declare@user2nvarchar(50)
select@user2=NamefromST_UserwhereID=1
print@user2
--使用update語句賦值
declare@user3nvarchar(50)
updateST_Userset@user3=NamewhereID=1
print@user3
二、表、臨時表、表變數
--創建臨時表1
createtable#DU_User1
(
[ID][int]NOTNULL,
[Oid][int]NOTNULL,
[Login][nvarchar](50)NOTNULL,
[Rtx][nvarchar](4)NOTNULL,
[Name][nvarchar](5)NOTNULL,
[Password][nvarchar](max)NULL,
[State][nvarchar](8)NOTNULL
);
--向臨時表1插入一條記錄
insertinto#DU_User1(ID,Oid,[Login],Rtx,Name,[Password],State)values(100,2,'LS','0000','臨時','321','特殊');
--從ST_User查詢數據,填充至新生成的臨時表
select*into#DU_User2fromST_UserwhereID<8
--查詢並聯合兩臨時表
select*from#DU_User2whereID<3unionselect*from#DU_User1
--刪除兩臨時表
droptable#DU_User1
droptable#DU_User2
--創建臨時表
CREATETABLE#t
(
[ID][int]NOTNULL,
[Oid][int]NOTNULL,
[Login][nvarchar](50)NOTNULL,
[Rtx][nvarchar](4)NOTNULL,
[Name][nvarchar](5)NOTNULL,
[Password][nvarchar](max)NULL,
[State][nvarchar](8)NOTNULL,
)
--將查詢結果集(多條數據)插入臨時表
insertinto#tselect*fromST_User
--不能這樣插入
--select*into#tfromdbo.ST_User
--添加一列,為int型自增長子段
altertable#tadd[myid]intNOTNULLIDENTITY(1,1)
--添加一列,默認填充全球唯一標識
altertable#tadd[myid1](newid())
select*from#t
droptable#t
--給查詢結果集增加自增長列
--無主鍵時:
selectIDENTITY(int,1,1)asID,Name,[Login],[Password]into#tfromST_User
select*from#t
--有主鍵時:
select(selectSUM(1)fromST_UserwhereID<=a.ID)asmyID,*fromST_UseraorderbymyID
--定義表變數
declare@ttable
(
idintnotnull,
msgnvarchar(50)null
)
insertinto@tvalues(1,'1')
insertinto@tvalues(2,'2')
select*from@t
三、循環
--while循環計算1到100的和
declare@aint
declare@sumint
set@a=1
set@sum=0
while@a<=100
begin
set@sum+=@a
set@a+=1
end
print@sum
四、條件語句
--if,else條件分支
if(1+1=2)
begin
print'對'
end
else
begin
print'錯'
end
--whenthen條件分支
declare@todayint
declare@weeknvarchar(3)
set@today=3
set@week=case
when@today=1then'星期一'
when@today=2then'星期二'
when@today=3then'星期三'
when@today=4then'星期四'
when@today=5then'星期五'
when@today=6then'星期六'
when@today=7then'星期日'
else'值錯誤'
end
print@week
五、游標
declare@IDint
declare@Oidint
declare@Loginvarchar(50)
--定義一個游標
declareuser_curcursorforselectID,Oid,[Login]fromST_User
--打開游標
openuser_cur
while@@fetch_status=0
begin
--讀取游標
fetchnextfromuser_curinto@ID,@Oid,@Login
print@ID
--print@Login
end
closeuser_cur
--摧毀游標
deallocateuser_cur
六、觸發器
觸發器中的臨時表:
Inserted
存放進行insert和update操作後的數據
Deleted
存放進行delete和update操作前的數據
--創建觸發器
CreatetriggerUser_OnUpdate
OnST_User
forUpdate
As
declare@msgnvarchar(50)
--@msg記錄修改情況
select@msg=N'姓名從「'+Deleted.Name+N'」修改為「'+Inserted.Name+'」'fromInserted,Deleted
--插入日誌表
insertinto[LOG](MSG)values(@msg)
--刪除觸發器
droptriggerUser_OnUpdate
七、存儲過程
--創建帶output參數的存儲過程
CREATEPROCEDUREPR_Sum
@aint,
@bint,
@sumintoutput
AS
BEGIN
set@sum=@a+@b
END
--創建Return返回值存儲過程
CREATEPROCEDUREPR_Sum2
@aint,
@bint
AS
BEGIN
Return@a+@b
END
--執行存儲過程獲取output型返回值
declare@mysumint
executePR_Sum1,2,@mysumoutput
print@mysum
--執行存儲過程獲取Return型返回值
declare@mysum2int
execute@mysum2=PR_Sum21,2
print@mysum2八、自定義函數
函數的分類:
1)標量值函數
2)表值函數
a:內聯表值函數
b:多語句表值函數
3)系統函數--新建標量值函數
createfunctionFUNC_Sum1
(
@aint,
@bint
)
returnsint
as
begin
return@a+@b
end
--新建內聯表值函數
createfunctionFUNC_UserTab_1
(
@myIdint
)
returnstable
as
return(select*fromST_UserwhereID<@myId)
--新建多語句表值函數
createfunctionFUNC_UserTab_2
(
@myIdint
)
returns@ttable
(
[ID][int]NOTNULL,
[Oid][int]NOTNULL,
[Login][nvarchar](50)NOTNULL,
[Rtx][nvarchar](4)NOTNULL,
[Name][nvarchar](5)NOTNULL,
[Password][nvarchar](max)NULL,
[State][nvarchar](8)NOTNULL
)
as
begin
insertinto@tselect*fromST_UserwhereID<@myId
return
end
--調用表值函數
select*fromdbo.FUNC_UserTab_1(15)
--調用標量值函數
declare@sint
set@s=dbo.FUNC_Sum1(100,50)
print@s
--刪除標量值函數
dropfunctionFUNC_Sum1
談談自定義函數與存儲過程的區別:
一、自定義函數:
1.可以返回表變數
2.限制頗多,包括
不能使用output參數;
不能用臨時表;
函數內部的操作不能影響到外部環境;
不能通過select返回結果集;
不能update,delete,資料庫表;
3.必須return一個標量值或表變數
自定義函數一般用在復用度高,功能簡單單一,爭對性強的地方。
二、存儲過程
1.不能返回表變數
2.限制少,可以執行對資料庫表的操作,可以返回數據集
3.可以return一個標量值,也可以省略return
存儲過程一般用在實現復雜的功能,數據操縱方面。
Ⅳ sqlserver如何寫存儲過程
create proc test ------創建存儲過程 test
@a int =『』-----------創建變數 有的存儲過程不需要變數,這個看個人所需要
as ---------------執行以下語句
select * from table where a=@a -------------------後面寫自己需要的語句
go
--------exec test 『1』----------執行存儲過程
Ⅳ sql server 2008 怎麼編寫存儲過程
第一步:點擊資料庫下的「可編程性」,選擇「存儲過程」,點擊滑鼠右鍵,選擇「新建存儲過程」
第二步:在create
PROCEDURE
後
輸入存儲過程的名字,緊跟著的就是定義存儲過程的參數,接下來就可以去編寫自己所需要組裝的存儲過程語句了
第三步:
編譯存儲過程,在工具欄上按下執行按鈕,如果沒有錯誤,就編寫成功了。
第四步:調用:在sqlserver的語句查詢框中,輸入exec
存儲過程名
參數,執行就可以了。
基本語法格式如下:中括弧帶的是可選項
create
proc
|
procere
pro_name
[{@參數數據類型}
[=默認值]
[output],
{@參數數據類型}
[=默認值]
[output],
....
]
as
begin
SQL_statements
--業務處理
end
Ⅵ 在SQL中存儲過程的一般語法是什麼
1、 創建語法
createproc|procerepro_name
[{@參數數據類型}[=默認值][output],
{@參數數據類型}[=默認值][output],
....
]
as
SQL_statements
2、 創建不帶參數存儲過程
--創建存儲過程
if(exists(select*fromsys.objectswherename='proc_get_student'))
dropprocproc_get_student
go
createprocproc_get_student
as
select*fromstudent;
--調用、執行存儲過程
execproc_get_student;
3、 修改存儲過程
--修改存儲過程
alterprocproc_get_student
as
select*fromstudent;
4、 帶參存儲過程
--帶參存儲過程
if(object_id('proc_find_stu','P')isnotnull)
dropprocproc_find_stu
go
createprocproc_find_stu(@startIdint,@endIdint)
as
select*fromstudentwhereidbetween@startIdand@endId
go
execproc_find_stu2,4;
5、 帶通配符參數存儲過程
--帶通配符參數存儲過程
if(object_id('proc_findStudentByName','P')isnotnull)
dropprocproc_findStudentByName
go
createprocproc_findStudentByName(@namevarchar(20)='%j%',@nextNamevarchar(20)='%')
as
select*fromstudentwherenamelike@nameandnamelike@nextName;
go
execproc_findStudentByName;execproc_findStudentByName'%o%','t%';
(6)sqlserver存儲過程寫法擴展閱讀:
SQL存儲過程優點:
1、重復使用。存儲過程可以重復使用,從而可以減少資料庫開發人員的工作量。
2、減少網路流量。存儲過程位於伺服器上,調用的時候只需要傳遞存儲過程的名稱以及參數就可以了,因此降低了網路傳輸的數據量。
3、安全性。參數化的存儲過程可以防止SQL注入式攻擊,而且可以將Grant、Deny以及Revoke許可權應用於存儲過程。
Ⅶ Sqlserver 2008 存儲過程 怎麼寫
第一步:點擊資料庫下的「可編程性」,選擇「存儲過程」,點擊滑鼠右鍵,選擇「新建存儲過程」
第二步:在create PROCEDURE 後 輸入存儲過程的名字,緊跟著的就是定義存儲過程的參數,接下來就可以去編寫自己所需要組裝的存儲過程語句了
第三步: 編譯存儲過程,在工具欄上按下執行按鈕,如果沒有錯誤,就編寫成功了。
第四步:調用:在sqlserver的語句查詢框中,輸入exec 存儲過程名 參數,執行就可以了。
基本語法格式如下:中括弧帶的是可選項
create proc | procere pro_name
[{@參數數據類型} [=默認值] [output],
{@參數數據類型} [=默認值] [output],
....
]
as
begin
SQL_statements
--業務處理
end
Ⅷ sqlserver怎麼創建存儲過程
如何創建存儲過程
在對象資源管理器中,連接到某個資料庫引擎實例,再展開該實例。
展開「資料庫」、存儲過程所屬的資料庫以及「可編程性」。
右鍵單擊「存儲過程」,再單擊「新建存儲過程」。
在「查詢」菜單上,單擊「指定模板參數的值」。
在「指定模板參數的值」對話框中,「值」列包含參數的建議值。接受這些值或將其替換為新值,再單擊「確定」。
在查詢編輯器中,使用過程語句替換 SELECT 語句。
若要測試語法,請在「查詢」菜單上,單擊「分析」。
若要創建存儲過程,請在「查詢」菜單上,單擊「執行」。
若要保存腳本,請在「文件」菜單上,單擊「保存」。接受該文件名或將其替換為新的名稱,再單擊「保存」。