如何使用函数初始化存储过程中的变量

发布于 2024-11-09 10:35:04 字数 1753 浏览 0 评论 0原文

如何使用函数初始化存储过程中的变量?

这不起作用:

/****** Object:  StoredProcedure [dbo].[usp_ShowBackpopGaps]    Script Date: 05/25/2011 19:57:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[usp_ShowBackpopGaps] 
    -- Add the parameters for the stored procedure here
    @StartDate datetime = DateAdd(yy, -1,getdate()), 
    @EndDate datetime = getdate
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    ;with dateranges as(
                select DateAdd(dd,-1,EtlStartDate) as DateFrom,
                DateAdd(dd,1,EtlEndDate) as DateTo
                from EtlJobRunStatus
                where JobRunStepID like 'ETL[0-9]%' and EndTime is not null
                union all
                select @StartDate ,@StartDate 
                union all
                select DateAdd(dd,-1,@EndDate),DateAdd(dd,-1,@EndDate)    
            )
            select DateAdd(dd,-1,DateTo) as MissingFrom,
              DateAdd(dd,1,NextDateFrom) as MissingTo,
              DateDiff(d,DateTo, NextDateFrom) as MissingDays
            from (
              select distinct DateFrom, DateTo as DateTo, 
                 (select MIN (dateFrom) 
                    from dateranges 
                    where DateTo > D.DateTo
                 ) as NextDateFrom
              from dateranges D
            ) X
            where DateTo < NextDateFrom
END


GO

How do you initialize a variable in a stored procedure with a function?

This doesn't work:

/****** Object:  StoredProcedure [dbo].[usp_ShowBackpopGaps]    Script Date: 05/25/2011 19:57:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[usp_ShowBackpopGaps] 
    -- Add the parameters for the stored procedure here
    @StartDate datetime = DateAdd(yy, -1,getdate()), 
    @EndDate datetime = getdate
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    ;with dateranges as(
                select DateAdd(dd,-1,EtlStartDate) as DateFrom,
                DateAdd(dd,1,EtlEndDate) as DateTo
                from EtlJobRunStatus
                where JobRunStepID like 'ETL[0-9]%' and EndTime is not null
                union all
                select @StartDate ,@StartDate 
                union all
                select DateAdd(dd,-1,@EndDate),DateAdd(dd,-1,@EndDate)    
            )
            select DateAdd(dd,-1,DateTo) as MissingFrom,
              DateAdd(dd,1,NextDateFrom) as MissingTo,
              DateDiff(d,DateTo, NextDateFrom) as MissingDays
            from (
              select distinct DateFrom, DateTo as DateTo, 
                 (select MIN (dateFrom) 
                    from dateranges 
                    where DateTo > D.DateTo
                 ) as NextDateFrom
              from dateranges D
            ) X
            where DateTo < NextDateFrom
END


GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2024-11-16 10:35:04

您不能将函数调用作为默认参数。

我想你需要

CREATE PROCEDURE [dbo].[usp_ShowBackpopGaps] 
 @StartDate DATETIME = NULL,      
 @EndDate DATETIME = NULL
AS 
BEGIN
SET @StartDate = ISNULL(@StartDate,DATEADD(yy, -1,GETDATE()))
SET @EndDate =  ISNULL(@EndDate, GETDATE())
...

You can't have a function call as a parameter default.

I think you need

CREATE PROCEDURE [dbo].[usp_ShowBackpopGaps] 
 @StartDate DATETIME = NULL,      
 @EndDate DATETIME = NULL
AS 
BEGIN
SET @StartDate = ISNULL(@StartDate,DATEADD(yy, -1,GETDATE()))
SET @EndDate =  ISNULL(@EndDate, GETDATE())
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文