2011-01-28 66 views
-1

請參閱下面的一行:查詢有什麼問題?

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

我stucked在一個非常愚蠢的事情,那是初學者也可以做的很好了。我需要更新上面的行。我已經以各種方式提出了相同的解決方案,但尚未實現。我不知道爲什麼????。

讓我解釋一下上面

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 

UPDATE:

事實上,也有少數情況 **

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. 

** 
+1

我對當年四月的第一個含義混淆,在我看來觸摸,這意味着`2011.04.01`但是`(選擇DATEADD(一個月什麼, datediff(month,0,getdate()),'18990401'))`returns 2010.04.01 - 這是預期的行爲嗎? – Robb 2011-01-28 14:45:11

+0

值得一提的是,您的查詢引用了錯誤的列名稱。它可能應該是'UPDATE tblStickerInfo SET Status = 3 WHERE FID = 46128 AND IssueDate <@FirstAprilOfCurrentYear AND St​​atus = 1`。 – 2011-01-28 14:45:56

+0

如果您在四天內運行此操作,變量`@ FirstAprilOfCurrentYear`將包含去年的第一個(2010-05-01) – 2011-01-28 15:07:37

回答

4

@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 
3

WHERE FisherId = 46128 AND

您的意思是硬編碼Fischerid的價值?你不應該使用輸入變量嗎?