1. 程式人生 > >MSSQL建立儲存過程

MSSQL建立儲存過程

--建立無參儲存過程

--檢測是否存在要建立的儲存過程,如果存在將其刪除
IF EXISTS(SELECT * FROM sysobjects WHERE name='StaffInfoSel')
DROP PROC StaffInfoSel
--刪除之後要加入批處理的GO
GO
CREATE PROCEDURE StaffInfoSel
AS
    SELECT StaffNum,StaffName,StaffPhone,StaffDepartment.StaffDepartName,StaffRemark,CASE StaffInfo.StaffState WHEN 'True' THEN
'啟用' WHEN 'False' THEN '停用' END newStaffState,CASE StaffInfo.IsLeader WHEN 'True' THEN '是' WHEN 'False' THEN '否' END newIsLeader,CASE StaffInfo.IsAdmin WHEN 'True' THEN '是' WHEN 'False' THEN '否' END newIsAdmin FROM StaffInfo LEFT JOIN StaffDepartment ON StaffInfo.StaffDepartID = StaffDepartment.StaffDepartID GO
--建立有參 --檢測是否存在要建立的儲存過程,如果存在將其刪除 IF EXISTS(SELECT * FROM sysobjects WHERE name='StaffInfoSel') DROP PROC StaffInfoSel --刪除之後要加入批處理的GO GO CREATE PROCEDURE StaffInfoSel @hero nvarchar(20) = NULL AS IF @hero IS NULL BEGIN END SELECT StaffNum,StaffName,StaffPhone,StaffDepartment.StaffDepartName,StaffRemark,CASE
StaffInfo.StaffState WHEN 'True' THEN '啟用' WHEN 'False' THEN '停用' END newStaffState,CASE StaffInfo.IsLeader WHEN 'True' THEN '是' WHEN 'False' THEN '否' END newIsLeader,CASE StaffInfo.IsAdmin WHEN 'True' THEN '是' WHEN 'False' THEN '否' END newIsAdmin FROM StaffInfo LEFT JOIN StaffDepartment ON StaffInfo.StaffDepartID = StaffDepartment.StaffDepartID GO