2014-02-13 102 views
2

我創建了以下接受2參數的存儲過程。有沒有辦法將參數與表中的特定列進行比較?基本上要用戶輸入proj_num和newDate,如果他們在表中顯示一些信息。存儲過程中的參數比較

CREATE PROCEDURE empInfo2 
(
    @proj_num AS char(3) 
    ,@newDate AS smalldatetime 
) 
AS 
BEGIN 
/* Insert IF Statement here Intent is to display an ERROR message 
if the proj_num and/or date are not within the tables*/ 

IF (@proj_num <> /*Compare to PROJ_NUM column to see if number is in range*/ 
    OR /* I assume @newDate must be compared to ASSIGN_DATE?? */) 
    PRINT 'Invalid input. Enter correct Project Number and Date.' 


SELECT EMPLOYEE.EMP_NUM 
     ,EMPLOYEE.EMP_FNAME 
     ,EMPLOYEE.EMP_LNAME 
     ,PROJECT.PROJ_NUM 
     ,ASSIGNMENT.ASSIGN_DATE 

FROM ASSIGNMENT INNER JOIN EMPLOYEE ON 
     ASSIGNMENT.EMP_NUM = EMPLOYEE.EMP_NUM INNER JOIN 
     PROJECT ON ASSIGNMENT.PROJ_NUM = PROJECT.PROJ_NUM 

WHERE PROJECT.PROJ_NUM = @proj_num AND ASSIGN_DATE = @newDate 
END 
+4

是否與你的代碼有問題嗎? –

+0

如果數據不在表中,則select將返回零行。那有什麼問題? – Luaan

+0

如果沒有行匹配,則需要將錯誤顯示到屏幕上。在另一個正常工作的過程中使用類似的語句。這個有點不同。 – allendks45

回答

1
IF (@proj_num NOT IN (SELECT ...)) OR (@newDate NOT IN (SELECT ...)) 
    PRINT 'Invalid input. Enter correct Project Number and Date.' 
0

它可以包含一些語法錯誤,但這個想法是

CREATE PROCEDURE empInfo2 
(
    @proj_num AS char(3) 
    ,@newDate AS smalldatetime 
) 
AS 
BEGIN 
/* Insert IF Statement here Intent is to display an ERROR message 
if the proj_num and/or date are not within the tables*/ 

    IF (NOT EXISTS (
     SELECT 1 FROM PROJECT WHERE PROJ_NUM = @proj_num 
           AND ASSIGN_DATE = @newDate) 
    BEGIN 
     PRINT 'Invalid input. Enter correct Project Number and Date.' 
    END 
    ELSE 

    SELECT EMPLOYEE.EMP_NUM 
    ,EMPLOYEE.EMP_FNAME 
    ,EMPLOYEE.EMP_LNAME 
    ,PROJECT.PROJ_NUM 
    ,ASSIGNMENT.ASSIGN_DATE 

    FROM ASSIGNMENT INNER JOIN EMPLOYEE ON 
    ASSIGNMENT.EMP_NUM = EMPLOYEE.EMP_NUM INNER JOIN 
     PROJECT ON ASSIGNMENT.PROJ_NUM = PROJECT.PROJ_NUM 

    WHERE PROJECT.PROJ_NUM = @proj_num AND ASSIGN_DATE = @newDate 

    END