日期:2014-05-18  浏览次数:20448 次

关于存储过程 if else
CREATE PROCEDURE GET_T_STAFF
(
 @_StaffCode INT
)
AS BEGIN 
declare @_sql varchar(100)
set @_sql='select'
  set @_sql=@_sql+' StaffCode,LoginPw,StaffName,Sex,IDCode,GraduateSchool,EntryDate,IfStop FROM dbo.T_Staff WHERE 1=1'
if @_StaffCode >=1
begin
  set @_sql=@_sql+' and StaffCode=@_StaffCode'
end
  print @_sql

end

 这里GET_T_STAFF 输入参数 并没有进入IF判断 求解释

------解决方案--------------------
SQL code
CREATE PROCEDURE GET_T_STAFF @_StaffCode as INT
AS 
BEGIN  
  declare @_sql as varchar(800)
  set @_sql = 'select'
  set @_sql = @_sql + ' StaffCode , LoginPw , StaffName , Sex , IDCode , GraduateSchool , EntryDate , IfStop FROM dbo.T_Staff WHERE 1 = 1 '
  if @_StaffCode >=1
     set @_sql=@_sql + ' and StaffCode = ' + ltrim(@_StaffCode)
  print @_sql
end