当我插入Emp时,任何人都可以修改它。命名它去第一次搜索,它不会检查其他参数你可以根据参数修改此sp进行精确搜索。
--select * from Training_TRNS
--USP_SearchEmployee '','2008-04-18 00:00:00.000','','','','',''
alter Procedure USP_SearchEmployee
@EmpName varchar(100)=null,
@DateFrom varchar(100)=null,
@DateTo varchar(100)=null,
@CourseName varchar(100)=null,
@JobFunction varchar(100)=null,
@Region varchar(100)=null,
@Status varchar(100)=null
AS
BEGIN
if (@EmpName!='' and @EmpName is not null)
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where EmpName like '%'+@EmpName+'%'
END
ELSE IF (@CourseName!='' and @CourseName is not null)
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where SpeCourse_ID like '%'+@CourseName+'%'
END
ELSE IF (@JobFunction!='' and @JobFunction is not null)
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where EmpJobFunction like '%'+@JobFunction+'%'
END
ELSE IF (@Region!='' and @Region is not null)
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where EmpRegion like '%'+@Region+'%'
END
ELSE IF (@Status!='' and @Status is not null)
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where Status like '%'+@Status+'%'
END
ELSE IF (@DateFrom!='' and @DateFrom is not null)
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where convert(varchar,DueDate,101) like '%'+convert(varchar,@DateFrom,101)+'%'
END
Else
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS
END
END
答案 0 :(得分:0)
试试这个。
alter Procedure USP_SearchEmployee
@EmpName varchar(100)=null,
@DateFrom varchar(100)=null,
@DateTo varchar(100)=null,
@CourseName varchar(100)=null,
@JobFunction varchar(100)=null,
@Region varchar(100)=null,
@Status varchar(100)=null
AS
BEGIN
select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS
where
EmpName = isnull(ltrim(rtrim(@EmpName)), EmpName) or EmpName like '%'+@EmpName+'%'
and SpeCourse_ID = isnull(ltrim(rtrim(@CourseName)), SpeCourse_ID) or SpeCourse_ID like '%'+@CourseName+'%'
and EmpJobFunction = isnull(ltrim(rtrim(@JobFunction)), EmpJobFunction) or EmpJobFunction like '%'+@JobFunction+'%'
and EmpRegion = isnull(ltrim(rtrim(@Region)), EmpRegion) or EmpRegion like '%'+@Region+'%'
and status = isnull(ltrim(rtrim(@status)), status) or status like '%'+@status+'%'
and DueDate = isnull(@DateFrom, DueDate) or convert(varchar,DueDate,101) like '%'+convert(varchar,@DateFrom,101)+'%'
END
答案 1 :(得分:0)
尝试这样的事情:
DECLARE @sql nvarchar(MAX) -- this would work only is this is SQL2005/2008, else nvarchar(4000)
SET @sql='select EmpName,convert(varchar,DueDate,101) as DueDate,SpeCourse_ID as CourseName,
EmpJobFunction as JOBFunction,EmpRegion as Region,Status
from Training_TRNS where 1=1 '
IF (@EmpName!='' and @EmpName is not null)
SET @sql=@sql + ' AND EmpName like ''%'+ @EmpName+ '%'''
IF (@CourseName!='' and @CourseName is not null)
SET @sql=@sql + ' AND CourseName like ''%'+ @CourseName+ '%'''
IF (@JobFunction!='' and @JobFunction is not null)
SET @sql=@sql + ' AND EmpJobFunction like ''%'+ @JobFunction + '%'''
IF (@Region!='' and @Region is not null)
SET @sql=@sql + ' AND EmpRegion like ''%'+ @Region+ '%'''
IF (@Status!='' and @Status is not null)
SET @sql=@sql + ' AND Status like ''%'+ @Status+ '%'''
IF (@DateFrom!='' and @DateFrom is not null)
SET @sql=@sql + ' AND convert(varchar,DueDate,101) like ''%'+convert(varchar,@DateFrom,101)+ '%'''
EXEC sp_executesql @sql