IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_wrk_time]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_wrk_time]
GO
CREATE procedure [dbo].[mps_save_wrk_time] 
@wrkid int,
@sqltext text,
@worktime varchar(20),
@week varchar(50),
@days varchar(100),
@times varchar(200) 
as
begin
declare @t table(type1 varchar(20),time1 int,time2 int) 
if (charindex('0',@worktime)>0) insert into @t(type1,time1,time2) values('work',0,0)
if (charindex('1',@worktime)>0) insert into @t(type1,time1,time2) values('offwork',0,0)
declare @s varchar(500) 
set @s = ','+@week+',' 
declare @i int 
set @i = 1 
while (@i<=7)
begin
if (charindex(','+ltrim(rtrim(str(@i)))+',',@s)>0) insert into @t(type1,time1,time2) values('week',@i,0)
set @i = @i + 1
end 
set @s = ','+@days+',' 
set @i = 1 
while (@i<=31)
begin
if (charindex(','+ltrim(rtrim(str(@i)))+',',@s)>0) insert into @t(type1,time1,time2) values('day',@i,0)
set @i = @i + 1
end 
set @s = ','+@times+',' 
set @i = 0 
while (@i<=23)
begin
if (charindex(','+ltrim(rtrim(str(@i)))+':0,',@s)>0) insert into @t(type1,time1,time2) values('time',@i,0)
if (charindex(','+ltrim(rtrim(str(@i)))+':15,',@s)>0) insert into @t(type1,time1,time2) values('time',@i,15)
if (charindex(','+ltrim(rtrim(str(@i)))+':30,',@s)>0) insert into @t(type1,time1,time2) values('time',@i,30)
if (charindex(','+ltrim(rtrim(str(@i)))+':45,',@s)>0) insert into @t(type1,time1,time2) values('time',@i,45)
set @i = @i + 1
end
delete mps_flowwork_worktime where wt_wrkid=@wrkid 
insert into mps_flowwork_worktime(wt_wrkid,wt_type,wt_time,wt_time2)
select @wrkid,type1,time1,time2 
from @t 
exec mps_save_extsqljs @wrkid,0,'wrk.time',@sqltext
end