查询有什么问题?

时间:2011-01-28 14:31:05

标签: sql-server

请参阅以下行:

FID    |     IssueDate                   |  IssueType         |    Status
46128  |     2010-12-30 00:00:00.000     |     2              |     1   

我被困在一个非常愚蠢的事情上,初学者也可以做得很好。我需要更新上面的行。我已经以各种方式问过解决方案但是还没有实现。我不知道为什么????。

让我解释一下上面的

FID是我的主键,发行日期是我的贴纸发行日期,发行类型是我的贴纸类型,状态是贴纸的当前状态。

现在我想要的是检查4月1日当前年份的发行日期。如果系统日期从3月31日(即到期的最后固定日期)发生变化,则行的状态将自动过期,状态将从1更改为3.

我正在使用给定的查询,执行后说0行已更新。请指导我在哪里犯错。

ALTER PROCEDURE [dbo].[spMakeStickerVoid]
(
    @FisherId BIGINT,
    @ManipulatedByUser VARCHAR(50),
    @OPParam INT OUTPUT
)
AS

BEGIN

   DECLARE @CurrentDate as DATETIME,
           @FirstAprilOfCurrentYear AS DATETIME

    SET @FirstAprilOfCurrentYear = (select dateadd(month,datediff(month,0,getdate()) ,'18990401'));
    SET @CurrentDate = GETDATE();
    --SET @OPParam =0


        IF @CurrentDate >= @FirstAprilOfCurrentYear

            BEGIN

                UPDATE tblStickerInfo
                SET    StatusId = 3

                WHERE  FisherId = 46128 AND  (IssuedDate < @FirstAprilOfCurrentYear) AND StatusId = 1

            END
        --SET @OPParam =1



END

更新:

实际上,条件很少   **

 1. It should occur every year 1st April or onwards.

      2. All issue dates that are on or before 31st March of that year will get 
expires, status    =3

      3. So i put a check if current date is 1st april or greater than that. 
because there might be a condition if the system was switched off on 1st april 
and started working on 4 th April. So even in this case the stickers prior to 
1st april will have to expire. 

      4. For making my query clear, just take an example of Insurance policy ,
 which is only valid for a year. Like if i have registered it on 28th Jan2011, 
then it will automatically get expired on next year 27th Jan 2012 midnight.
 But here in my case date is fixed to 31st March midnight every year.

      5. Columns names and parameters are not an issue here, i had 
placed somewhat diff here consider FID = FisherId

      6. Need a query that will automatically populate the @FirstAprilOfEveryYear 
with first april of that year, so that it will be carried out automatically.

**

2 个答案:

答案 0 :(得分:3)

在哪里FisherId = 46128 AND

您的意思是硬编码Fischerid的价值吗?你不应该使用输入变量吗?

答案 1 :(得分:3)

@FirstAprilOfCurrentYear是2010年4月的第一天,这意味着(IssuedDate < @FirstAprilOfCurrentYear)不正确。

这可能会做你想要的事情

declare @FirstOfAprilCurrentYear datetime
set @FirstOfAprilCurrentYear = dateadd(year,datediff(year,0,getdate()) ,'1900-04-01') 

if @FirstOfAprilCurrentYear >= getdate()
begin
    -- Current date is After 31st March current year
    update tblstickerinfo
    set statusid = 3
    where IssuedDate < @FirstOfAprilCurrentYear
end