2013-07-30 58 views
0

的搜索ID,名和姓嗨我有用於搜索員工信息的此存儲過程。我希望用戶可以選擇輸入ID,或名字或姓氏。但是當我執行這個存儲過程時,它需要其他參數。在SQL中使用類似於

create proc searchtry 
    @empID int, 
    @firstname varchar(20), 
    @lastname varchar(20) 
    as 
    begin 
    select fld_EmployeeId,fld_EmployeeFirstName,fld_EmployeeLastName 
    from Reference.tbl_EmployeeDetails 
    where fld_EmployeeId like @empID 
    OR fld_EmployeeFirstName like @firstname 
    OR fld_EmployeeLastName like @lastname 
    end 
+0

的[我怎樣才能在T-SQL存儲過程中使用可選參數?(可能重複http://stackoverflow.com/questions/3415582 /如何使用可選參數在SQL存儲過程) – bendataclear

+0

每當你使用存儲過程,你應該提供你聲明的所有參數 – zxc

回答

0

你應該給默認值的參數

create proc searchtry 
    @empID int = NULL, 
    @firstname varchar(20) = '', 
    @lastname varchar(20) = '' 
    as 
    begin 
    select fld_EmployeeId,fld_EmployeeFirstName,fld_EmployeeLastName 
    from Reference.tbl_EmployeeDetails 
    where fld_EmployeeId like @empID 
    OR fld_EmployeeFirstName like @firstname 
    OR fld_EmployeeLastName like @lastname 
    end