存儲過程規范寫法



--1.判斷存儲過程是否存在,存在則刪除
IF
OBJECT_ID('存儲過程名稱', 'P') IS NOT NULL DROP PROCEDURE dbo.usp_GetMeasureAnalysisIssueReminder_Query(存儲過程名) GO

--2.這些是 SQL-92 設置語句,使 SQL Server 2000/2005 遵從 SQL-92 規則。可不寫
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO

--3.創建存儲過程並聲明傳入參數 CREATE PROCEDURE usp_GetMeasureAnalysisIssueReminder_Query( @companyCode NVARCHAR(30) ) AS

--4.編寫存儲過程內容
BEGIN declare @lastMonth varchar(20) declare @currentMonth varchar(20) set @lastMonth=(select CONVERT(varchar(7), dateadd(mm,-1,getdate()) , 120) + '-1') set @currentMonth=(select CONVERT(varchar(7), getdate() , 120) + '-1') ;with cr as ( select distinct PassNo from MS_AssetGroupMeasure agm inner join MS_MeasureAnalysis ma on agm.Id = ma.AssetGroupMeasureId where ma.MeasureResult='Fail' and agm.PassDate between @lastMonth and @currentMonth AND agm.CompanyCode = @companyCode ) select ag.Name ,agm.PassNo ,agm.BatchNo ,ma.SampleName ,ma.Item ,ma.AddedDate ,mr.CodeDescription AS MeasureResult ,CONVERT(varchar(20),CONVERT(DECIMAL(13,3),ma.RandR)) AS RandR ,CONVERT(varchar(20),CONVERT(DECIMAL(13,3),ma.Ndc)) AS Ndc from MS_AssetGroupMeasure agm inner join AMG_AssetGroup ag on ag.Id = agm.AssetGroupId and ag.IsDeleted=0 inner join MS_MeasureAnalysis ma on agm.Id=ma.AssetGroupMeasureId and ma.IsDeleted=0 LEFT JOIN dbo.SYS_CodeTable mr ON ma.MeasureResult=mr.Code AND mr.IsDeleted=0 AND mr.CodeCategory = 'MeasureResult' where agm.IsDeleted=0 and agm.PassNo in (select * from cr) order by agm.PassNo DESC,ma.AddedDate DESC END

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM