2016-06-08 56 views
0

如果創建的日期時間是空的或沒有值,我想返回整個表。如果它包含日期,我想返回表格數據大於那個日期。在這裏下面我提交一個代碼,但它提供數據時,有一個參數,但它顯示這樣的錯誤過程SelectRotarian_AllByCreatedDatetime 程序或功能SelectRotarian_AllByCreatedDatetime預計參數@createdDateTime,這是沒有提供。傳遞null參數時出錯

(1行(S)的影響)

ALTER PROCEDURE [dbo].[SelectRotarian_AllByCreatedDatetime] 
    @createdDateTime varchar(50) 
AS 
BEGIN 

    SET NOCOUNT ON; 

    if (Coalesce(@createdDateTime,'') = '') 
    begin 
      select [id] 
     ,[surName] 
     ,[firstName] 
     ,[secondName] 
     , [address] 
     , [gender] 
     ,[picture] 
     ,[email] 
     ,[phoneNumber] 
     ,[DOB] 
     ,[vocation] 
     ,[bloodGroup_id] 
     ,[club_id] 
     ,[createdDateTime] 
    FROM [rotary_rotarian] 
    end 
    else 
    begin 

      SELECT[id] 
     ,[surName] 
     ,[firstName] 
     ,[secondName] 
     , [address] 
     , [gender] 
     ,[picture] 
     ,[email] 
     ,[phoneNumber] 
     ,[DOB] 
     ,[vocation] 
     ,[bloodGroup_id] 
     ,[club_id] 
     ,[createdDateTime] 
    FROM [rotary_rotarian] 
    where [createdDateTime] > CAST(@createdDateTime AS DateTime) 
     end 
    END 

所以請幫我

回答

1

錯誤消息說,:根據您當前的存儲過程,你必須傳遞一個參數。即使你傳遞一個空參數或一個NULL參數,你仍然需要傳遞一個參數。獲得錯誤的唯一方法是調用該過程而不傳遞參數。

如果你想如果沒有參數傳遞您的存儲過程來連工作,那麼你必須給參數的默認值,就像這樣:

ALTER PROCEDURE [dbo].[SelectRotarian_AllByCreatedDatetime] 
    @createdDateTime varchar(50) = '' 
AS 
+0

感謝ü這麼多。現在它正在工作。你的巨大幫助... –

3

相反的:

ALTER PROCEDURE [dbo].[SelectRotarian_AllByCreatedDatetime] 
    @createdDateTime varchar(50) 
AS 
BEGIN 

讓你的參數可爲空/可選,如:

ALTER PROCEDURE [dbo].[SelectRotarian_AllByCreatedDatetime] 
    @createdDateTime DateTime = NULL 
AS 
BEGIN 

也注意,你應該在一個datetime路過爲datetime,不是varchar

則:

if (Coalesce(@createdDateTime,'') = '') 

可以成爲

if (@createdDatetime IS NULL)