让脚本每年 2 月 1 日查找一次记录

发布于 2025-01-20 02:03:29 字数 508 浏览 1 评论 0原文

我试图考虑一种方法(我是SQL的新手),以获取此脚本,以每年2月1日每年查找一次记录。任何帮助都很棒。

select permit.PERMITID, SAVERPERMITMANAGEMENT.permitrequested, PERMITSTATUS.NAME
from PERMIT
inner join SAVERPERMITMANAGEMENT
on permit.PERMITID = SAVERPERMITMANAGEMENT.ID
inner join PERMITSTATUS
on permitstatus.PERMITSTATUSID = permit.PERMITSTATUSID
where Typeofpermitrequested = 'GUID' AND permit.PERMITSTATUSID = 'GUID'
AND PERMITTYPEID = 'GUID' AND getdate() = 2022-02-01

我被困在日期部分,以及如何使脚本每年逐年逐年查看,直到关闭为止。这是针对年度报告。

I am trying to think of a way (I am pretty new to sql) to get this script to only look for records once every year on February 1st. Any help would be awesome.

select permit.PERMITID, SAVERPERMITMANAGEMENT.permitrequested, PERMITSTATUS.NAME
from PERMIT
inner join SAVERPERMITMANAGEMENT
on permit.PERMITID = SAVERPERMITMANAGEMENT.ID
inner join PERMITSTATUS
on permitstatus.PERMITSTATUSID = permit.PERMITSTATUSID
where Typeofpermitrequested = 'GUID' AND permit.PERMITSTATUSID = 'GUID'
AND PERMITTYPEID = 'GUID' AND getdate() = 2022-02-01

I am stuck at the date portion and how to have the script look each year incrementally until turned off. This is for yearly reports.

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

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

发布评论

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

评论(1

玩世 2025-01-27 02:03:29

根据我对您问题的理解,您需要将查询修改为以下内容:

IF DATEPART(MONTH, GETDATE()) = 2 -- Check if today's month is February
    AND DATEPART(DAY, GETDATE()) = 1 -- Check if today is the first of the month
BEGIN
    -- If it's the first of February, then return the query results

    select permit.PERMITID, SAVERPERMITMANAGEMENT.permitrequested, PERMITSTATUS.NAME
    from PERMIT
    inner join SAVERPERMITMANAGEMENT
    on permit.PERMITID = SAVERPERMITMANAGEMENT.ID
    inner join PERMITSTATUS
    on permitstatus.PERMITSTATUSID = permit.PERMITSTATUSID
    where Typeofpermitrequested = 'GUID' AND permit.PERMITSTATUSID = 'GUID'
    AND PERMITTYPEID = 'GUID';

END

但是,您需要每天运行该查询&除 2 月 1 日外,您每天都会得到空结果。

因此,更好的选择是使用 SQL Server Agent:创建一个包含上述 SELECT 查询的作业安排它在每年 2 月 1 日运行。

From what I understand of your question, you would need to amend your query to the following:

IF DATEPART(MONTH, GETDATE()) = 2 -- Check if today's month is February
    AND DATEPART(DAY, GETDATE()) = 1 -- Check if today is the first of the month
BEGIN
    -- If it's the first of February, then return the query results

    select permit.PERMITID, SAVERPERMITMANAGEMENT.permitrequested, PERMITSTATUS.NAME
    from PERMIT
    inner join SAVERPERMITMANAGEMENT
    on permit.PERMITID = SAVERPERMITMANAGEMENT.ID
    inner join PERMITSTATUS
    on permitstatus.PERMITSTATUSID = permit.PERMITSTATUSID
    where Typeofpermitrequested = 'GUID' AND permit.PERMITSTATUSID = 'GUID'
    AND PERMITTYPEID = 'GUID';

END

However, you'd need to run that query every day & you'd get an empty result every day except February 1st.

So, a better option would be to employ something like SQL Server Agent: create a job containing the above SELECT query & schedule it to run every year on February 1st.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文