Commit 95977bb3 by TongZuu

init sql script

parents
IF OBJECT_ID ( 'broker_assignProcess', 'P' ) IS NOT NULL
DROP PROCEDURE broker_assignProcess;
go
CREATE PROCEDURE broker_assignProcess
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL Serializable;
BEGIN TRANSACTION;
DECLARE @EmptyNum int;
DECLARE @SV varchar(20) , @QID varchar(20) , @TYPE varchar(2) , @GROUP varchar(2);
DECLARE @LockResult int;
EXECUTE @LockResult = sp_getapplock @Resource = 'Serializable_Process',@LockMode = 'Exclusive',
@LockOwner = 'Session',@LockTimeout = 0,@DbPrincipal = 'dbo';
IF @LockResult <> 0
BEGIN
ROLLBACK TRANSACTION
RAISERROR ( 51001, 16, 1 )
RETURN
END
SELECT
@EmptyNum = ISNULL(SUM(THREAD_PROCESS-PROCESS_NORMAL) , 0 )
FROM vw_csc_server WHERE SERVER_STATUS = '1'
PRINT 'WORKFLOW EMPIY is '
PRINT @EmptyNum;
if @EmptyNum > 0
BEGIN
DECLARE db_cursor_queue CURSOR FOR
WITH SIMCSC_QUQUE AS(
SELECT ROW_NUMBER() OVER(ORDER BY ququeid) AS ROWNUMBER,* FROM CSC_QUQUE WHERE PROCESS_TYPE != 'W' and PROCESS_GROUP = 'P' and PROCESS_STATUS = 'B'
)
SELECT QUQUEID,PROCESS_TYPE,PROCESS_GROUP FROM SIMCSC_QUQUE
WHERE ROWNUMBER <= @EmptyNum
ORDER BY PROCESS_PRIORITY,RUNNO,QUQUEID
OPEN db_cursor_queue
FETCH NEXT FROM db_cursor_queue INTO @QID,@TYPE,@GROUP
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'FETCH DATA [ '+@QID+' ][ '+@TYPE+' ][ '+@GROUP+' ]';
SET @SV = dbo.getReadyServer(@GROUP , @TYPE);
PRINT 'GET SERVER [ '+@SV+' ]';
IF @SV <> ''
BEGIN
PRINT 'IS UPDATE QUQUE [ '+@QID+' ][ '+@SV+' ]';
BEGIN TRANSACTION;
UPDATE CSC_QUQUE SET PROCESS_STATUS = 'T',SERVER_NAME = @SV WHERE QUQUEID = @QID;
COMMIT TRANSACTION;
BEGIN TRANSACTION;
UPDATE CSC_SERVER SET LAST_ASSIGN = CONVERT(VARCHAR , GETDATE() , 120) WHERE SERVER_NAME = @SV;
COMMIT TRANSACTION;
END
FETCH NEXT FROM db_cursor_queue INTO @QID,@TYPE,@GROUP
END
CLOSE db_cursor_queue
DEALLOCATE db_cursor_queue
END
EXECUTE sp_releaseapplock @Resource = 'Serializable_Process',@LockOwner = 'Session',@DbPrincipal = 'dbo';
COMMIT TRANSACTION;
END
GO
EXECUTE sp_addmessage
@msgnum = 51001,
@severity = 16,
@msgtext = N'Resource NOT Available',
@lang = 'us_english',
@replace = REPLACE
EXECUTE sp_addmessage
@msgnum = 51002,
@severity = 16,
@msgtext = N'ERROR UPDATE QUQUE !!!!!',
@lang = 'us_english',
@replace = REPLACE
\ No newline at end of file
IF OBJECT_ID ( 'broker_assignReport', 'P' ) IS NOT NULL
DROP PROCEDURE broker_assignReport;
go
CREATE PROCEDURE broker_assignReport
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL Serializable;
BEGIN TRANSACTION;
DECLARE @EmptyNum int;
DECLARE @SV varchar(20) , @QID varchar(20) , @TYPE varchar(2) , @GROUP varchar(2);
DECLARE @LockResult int;
EXECUTE @LockResult = sp_getapplock @Resource = 'Serializable_Report',@LockMode = 'Exclusive',@LockTimeout = 0,
@LockOwner = 'Session',@DbPrincipal = 'dbo';
IF @LockResult <> 0
BEGIN
ROLLBACK TRANSACTION
RAISERROR ( 51001, 16, 1 )
RETURN
END
SELECT
@EmptyNum = ISNULL(SUM(THREAD_REPORT-PROCESS_REPORT) , 0)
FROM vw_csc_server WHERE SERVER_STATUS = '1'
PRINT 'REPORT EMPIY is '
PRINT @EmptyNum;
if @EmptyNum > 0
BEGIN
DECLARE db_cursor_queue CURSOR FOR
WITH SIMCSC_QUQUE AS(
SELECT ROW_NUMBER() OVER(ORDER BY ququeid) AS ROWNUMBER,* FROM CSC_QUQUE WHERE PROCESS_GROUP = 'R' and PROCESS_STATUS = 'B'
)
SELECT QUQUEID,PROCESS_TYPE,PROCESS_GROUP FROM SIMCSC_QUQUE
WHERE ROWNUMBER <= @EmptyNum
ORDER BY PROCESS_PRIORITY,RUNNO,QUQUEID
OPEN db_cursor_queue
FETCH NEXT FROM db_cursor_queue INTO @QID,@TYPE,@GROUP
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'FETCH DATA [ '+@QID+' ][ '+@TYPE+' ][ '+@GROUP+' ]';
SET @SV = dbo.getReadyServer(@GROUP , @TYPE);
PRINT 'GET SERVER [ '+@SV+' ]';
IF @SV <> ''
BEGIN
PRINT 'IS UPDATE QUQUE [ '+@QID+' ][ '+@SV+' ]';
BEGIN TRANSACTION;
UPDATE CSC_QUQUE SET PROCESS_STATUS = 'T',SERVER_NAME = @SV WHERE QUQUEID = @QID;
COMMIT TRANSACTION;
BEGIN TRANSACTION;
UPDATE CSC_SERVER SET LAST_ASSIGN = CONVERT(VARCHAR , GETDATE() , 120) WHERE SERVER_NAME = @SV;
COMMIT TRANSACTION;
END
FETCH NEXT FROM db_cursor_queue INTO @QID,@TYPE,@GROUP
END
CLOSE db_cursor_queue
DEALLOCATE db_cursor_queue
END
EXECUTE sp_releaseapplock @Resource = 'Serializable_Report',@LockOwner = 'Session',@DbPrincipal = 'dbo';
COMMIT TRANSACTION;
END
GO
IF OBJECT_ID ( 'broker_assignWorkflow', 'P' ) IS NOT NULL
DROP PROCEDURE broker_assignWorkflow;
go
CREATE PROCEDURE broker_assignWorkflow
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL Serializable;
BEGIN TRANSACTION;
DECLARE @EmptyNum int;
DECLARE @SV varchar(20) , @QID varchar(20) , @TYPE varchar(2) , @GROUP varchar(2);
DECLARE @LockResult int;
EXECUTE @LockResult = sp_getapplock @Resource = 'Serializable_Workflow',@LockMode = 'Exclusive',@LockTimeout = 0,
@LockOwner = 'Session',@DbPrincipal = 'dbo';
IF @LockResult <> 0
BEGIN
ROLLBACK TRANSACTION
RAISERROR ( 51001, 16, 1 )
RETURN
END
SELECT
@EmptyNum = ISNULL(SUM(THREAD_WORKFLOW-PROCESS_WORKFLOW),0)
FROM vw_csc_server WHERE SERVER_STATUS = '1'
PRINT 'WORKFLOW EMPIY is '
PRINT @EmptyNum;
if @EmptyNum > 0
BEGIN
DECLARE db_cursor_queue CURSOR FOR
WITH SIMCSC_QUQUE AS(
SELECT ROW_NUMBER() OVER(ORDER BY ququeid) AS ROWNUMBER,* FROM CSC_QUQUE WHERE PROCESS_TYPE = 'W' and PROCESS_GROUP = 'P' and PROCESS_STATUS = 'B'
)
SELECT QUQUEID,PROCESS_TYPE,PROCESS_GROUP FROM SIMCSC_QUQUE
WHERE ROWNUMBER <= @EmptyNum
ORDER BY PROCESS_PRIORITY,RUNNO,QUQUEID
OPEN db_cursor_queue
FETCH NEXT FROM db_cursor_queue INTO @QID,@TYPE,@GROUP
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'FETCH DATA [ '+@QID+' ][ '+@TYPE+' ][ '+@GROUP+' ]';
SET @SV = dbo.getReadyServer(@GROUP , @TYPE);
PRINT 'GET SERVER [ '+@SV+' ]';
IF @SV <> ''
BEGIN
PRINT 'IS UPDATE QUQUE [ '+@QID+' ][ '+@SV+' ]';
BEGIN TRANSACTION;
UPDATE CSC_QUQUE SET PROCESS_STATUS = 'T',SERVER_NAME = @SV WHERE QUQUEID = @QID;
COMMIT TRANSACTION;
BEGIN TRANSACTION;
UPDATE CSC_SERVER SET LAST_ASSIGN = CONVERT(VARCHAR , GETDATE() , 120) WHERE SERVER_NAME = @SV;
COMMIT TRANSACTION;
END
FETCH NEXT FROM db_cursor_queue INTO @QID,@TYPE,@GROUP
END
CLOSE db_cursor_queue;
DEALLOCATE db_cursor_queue ;
END
EXECUTE sp_releaseapplock @Resource = 'Serializable_Workflow',@LockOwner = 'Session',@DbPrincipal = 'dbo';
COMMIT TRANSACTION;
END
GO
IF OBJECT_ID ( 'checkServerOnline', 'P' ) IS NOT NULL
DROP PROCEDURE checkServerOnline;
GO
CREATE PROCEDURE checkServerOnline
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRANSACTION;
DECLARE @OVER_TIME int;
DECLARE @SV varchar(30);
SET @OVER_TIME = 2;
DECLARE @LockResult int;
EXECUTE @LockResult = sp_getapplock @Resource = 'checkServerOnline',@LockMode = 'Exclusive',@LockTimeout = 0;
IF @LockResult <> 0
BEGIN
ROLLBACK TRANSACTION
RAISERROR ( 51001, 16, 1 )
RETURN
END
DECLARE db_cursor_server CURSOR FOR
SELECT SERVER_NAME FROM CSC_SERVER WITH(NOLOCK) WHERE DATEDIFF(mi , LAST_ACTION , GETDATE() ) > @OVER_TIME;
OPEN db_cursor_server
FETCH NEXT FROM db_cursor_server INTO @SV
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRANSACTION;
UPDATE CSC_SERVER SET SERVER_STATUS = '2' WHERE SERVER_NAME = @SV;
COMMIT TRANSACTION;
BEGIN TRANSACTION ;
UPDATE CSC_QUQUE SET PROCESS_STATUS = CASE WHEN PROCESS_TYPE = 'R' THEN 'T' ELSE 'K' END WHERE SERVER_NAME = @SV AND PROCESS_STATUS = 'R';
COMMIT TRANSACTION;
BEGIN TRANSACTION;
UPDATE CSC_QUQUE SET PROCESS_STATUS = 'B',SERVER_NAME = '' WHERE SERVER_NAME = @SV AND PROCESS_STATUS = 'T' AND PROCESS_TYPE != 'R';
COMMIT TRANSACTION;
FETCH NEXT FROM db_cursor_server INTO @SV
END
CLOSE db_cursor_server;
DEALLOCATE db_cursor_server ;
EXECUTE sp_releaseapplock @Resource = 'checkServerOnline';
COMMIT TRANSACTION;
END
GO
IF OBJECT_ID ( 'CSC_SERVER', 'U' ) IS NOT NULL
DROP TABLE CSC_SERVER;
go
CREATE TABLE [dbo].[CSC_SERVER](
[HIS_START_DATETIME] [varchar](60) NULL,
[LAST_ASSIGN] [varchar](20) NULL,
[THREAD_REPORT] [int] NULL,
[SERVER_STATUS] [varchar](2) NULL,
[REMARK] [varchar](1000) NULL,
[THREAD_WORKFLOW] [int] NULL,
[LOAD_STATIC_CONFIG] [varchar](250) NULL,
[COMPANYID] [varchar](5) NOT NULL,
[START_DATETIME] [varchar](20) NULL,
[THREAD_PROCESS] [int] NULL,
[SERVER_PRODUCTION] [varchar](2) NULL,
[SERVER_NAME] [varchar](30) NOT NULL,
[LAST_ACTION] [varchar](20) NULL,
[ACTORID] [varchar](15) NULL,
[CREATE_TIME] [varchar](8) NULL,
[VERIFIED] [varchar](1) NULL,
[WF_STATUS] [varchar](1) NULL,
[EDIT_BY] [varchar](15) NULL,
[WF_REF_DOC] [varchar](20) NULL,
[CREATE_BY] [varchar](15) NULL,
[EDIT_DATE] [varchar](10) NULL,
[RUNNO] [varchar](30) NULL,
[APPROVE] [varchar](15) NULL,
[EDIT_TIME] [varchar](8) NULL,
[CREATE_DATE] [varchar](10) NULL,
PRIMARY KEY CLUSTERED
(
[SERVER_NAME] ASC,
[COMPANYID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
IF OBJECT_ID ( 'VW_CSC_SERVER', 'V' ) IS NOT NULL
DROP VIEW VW_CSC_SERVER;
go
CREATE VIEW [dbo].[VW_CSC_SERVER]
AS
SELECT S.SERVER_NAME, S.THREAD_PROCESS, SUM(CASE WHEN PROCESS_TYPE <> 'W' AND PROCESS_GROUP = 'P' AND PROCESS_TYPE <> 'R' THEN 1 ELSE 0 END)
AS PROCESS_NORMAL, S.THREAD_WORKFLOW, SUM(CASE WHEN PROCESS_TYPE = 'W' AND PROCESS_GROUP = 'P' THEN 1 ELSE 0 END)
AS PROCESS_WORKFLOW, S.THREAD_REPORT, SUM(CASE WHEN charindex(PROCESS_GROUP, 'RIEM') > 0 THEN 1 ELSE 0 END) AS PROCESS_REPORT,
S.LAST_ACTION, S.LAST_ASSIGN, S.SERVER_STATUS, S.START_DATETIME , S.SERVER_PRODUCTION
FROM dbo.CSC_SERVER AS S WITH(NOLOCK) LEFT OUTER JOIN
dbo.CSC_QUQUE AS Q WITH (NOLOCK) ON S.SERVER_NAME = Q.SERVER_NAME AND (Q.PROCESS_STATUS = 'R' OR
(S.SERVER_PRODUCTION = '1' AND Q.PROCESS_STATUS = 'T'))
where S.SERVER_STATUS = '1'
GROUP BY S.SERVER_NAME, S.THREAD_PROCESS, S.THREAD_WORKFLOW, S.THREAD_REPORT, S.LAST_ACTION, S.LAST_ASSIGN, S.SERVER_STATUS, S.START_DATETIME , S.SERVER_PRODUCTION
GO
IF OBJECT_ID ( 'LastAction', 'TR' ) IS NOT NULL
DROP trigger LastAction;
go
CREATE TRIGGER LastAction on CSC_SERVER FOR UPDATE AS
BEGIN
UPDATE CSC_SERVER SET LAST_ACTION = CONVERT(VARCHAR , GETDATE() , 120)
FROM Inserted Ins
WHERE CSC_SERVER.SERVER_NAME = Ins.SERVER_NAME AND Ins.LAST_ACTION = 'NOW'
END
go
\ No newline at end of file
IF OBJECT_ID ( 'broker_quque', 'TR' ) IS NOT NULL
DROP trigger broker_quque;
go
create trigger broker_quque on csc_quque
instead of INSERT as
BEGIN
DECLARE @SV_NAME varchar(20) , @group varchar(2),@type varchar(2) , @qid varchar(20) , @stat varchar(2);
DECLARE @process int;
SELECT @SV_NAME = inserted.server_name , @type = inserted.process_type , @group = inserted.process_group , @stat = inserted.process_status FROM inserted;
IF( @stat = 'T' or @stat = 'W' )
BEGIN
IF( @SV_NAME != '' and @type = 'W' )
BEGIN
SELECT @SV_NAME = SERVER_NAME FROM VW_CSC_SERVER WHERE SERVER_NAME = @SV_NAME AND THREAD_WORKFLOW > PROCESS_WORKFLOW
END
IF( @SV_NAME = '' or @SV_NAME = 'NONE')
BEGIN
SET @SV_NAME = dbo.getReadyServer(@group , @type);
PRINT 'getReadyServer ['+@group+' , '+@type+'] server_name = '+@SV_NAME
END
IF( @SV_NAME = '' and @type != 'R')
BEGIN
set @stat = 'B';
PRINT 'Status B'
END
ELSE
BEGIN
SET @stat = 'T';
PRINT 'Status T servername'+@SV_NAME
UPDATE CSC_SERVER SET LAST_ASSIGN = CONVERT(VARCHAR , GETDATE() , 120) WHERE SERVER_NAME = @SV_NAME;
END
END
INSERT INTO csc_quque(QUQUEID,PARENTID,TNAME,ENAME,PROCESS_STATUS,PROCESS_TYPE,PROCESS_GROUP,OWNER,CONDITION,REMOTE_IP,CLASS_NAME,SERVER_NAME,SCREEN_NAME,COMPANYID,START_TIME,END_TIME,PROCESS_MSG,EMPLOYEEID,USER_NAME,CANCLE_BY,CANCLE_TIME,CANCLE_MSG,REMARK,INTERVAL,INTERVAL_TYPE,NEXT_TIME,PROCESS_PRIORITY,RESULT_TYPE,PTYPE,CSC_LOG,EXPNAME,EXPFOLDER,PROVIDER_URL,HTTP_PORT,REFERKEY,TAG_INITMYPROCESS,EDIT_BY,EDIT_DATE,EDIT_TIME,CREATE_BY,CREATE_TIME,CREATE_DATE,ACTORID,APPROVE,VERIFIED,WF_STATUS,RUNNO,WF_REF_DOC)
SELECT QUQUEID,PARENTID,TNAME,ENAME,@stat,PROCESS_TYPE,PROCESS_GROUP,OWNER,CONDITION,REMOTE_IP,CLASS_NAME,@SV_NAME,SCREEN_NAME,COMPANYID,START_TIME,END_TIME,PROCESS_MSG,EMPLOYEEID,USER_NAME,CANCLE_BY,CANCLE_TIME,CANCLE_MSG,REMARK,INTERVAL,INTERVAL_TYPE,NEXT_TIME,PROCESS_PRIORITY,RESULT_TYPE,PTYPE,CSC_LOG,EXPNAME,EXPFOLDER,PROVIDER_URL,HTTP_PORT,REFERKEY,TAG_INITMYPROCESS,EDIT_BY,EDIT_DATE,EDIT_TIME,CREATE_BY,CREATE_TIME,CREATE_DATE,ACTORID,APPROVE,VERIFIED,WF_STATUS,RUNNO,WF_REF_DOC from inserted
end
\ No newline at end of file
IF OBJECT_ID ( 'getReadyServer', 'FN' ) IS NOT NULL
DROP function getReadyServer;
go
/*
@group = P = process
@type = W = workflow
@type = R = auto
@type = N = normall
@group = RIEM = report,import,export
*/
create function getReadyServer(@group varchar(1) , @type varchar(1))
returns varchar(max)
as
begin
declare @server varchar(max)
set @server = ''
if @group = 'P'
begin
if @type = 'W'
begin
select top 1 @server = server_name from vw_csc_server
where thread_workflow > process_workflow and server_production = '1'
order by process_workflow,last_assign
end
else
begin
select top 1 @server = server_name from vw_csc_server
where thread_process > process_normal and server_production = '1'
order by process_normal,last_assign
end
end
else
begin
select top 1 @server = server_name from vw_csc_server
where thread_report > process_report and server_production = '1'
order by process_report,last_assign
end
return @server
end
\ No newline at end of file
--Check Lock broker
--brokerProcess
SELECT APPLOCK_TEST('dbo', 'Serializable_Process', 'Exclusive', 'Session');
--brokerReport
SELECT APPLOCK_TEST('dbo', 'Serializable_Report', 'Exclusive', 'Session');
--brokerWorkflow
SELECT APPLOCK_TEST('dbo', 'Serializable_Workflow', 'Exclusive', 'Session');
--Cancel Lock broker
EXECUTE sp_releaseapplock @Resource = 'Serializable_Process',@LockOwner = 'Session',@DbPrincipal = 'dbo';
select * from vw_csc_server
--update csc_server set thread_process = '4',thread_workflow = '10',thread_report='10' where server_name = 'YUM8'
select * from csc_quque where process_status = 'B'
--update csc_quque set process_priority = '8' where process_status = 'B' and CLASS_NAME = 'ImportSwipeCard_yum'
select ququeid,parentid,tname,process_status,process_type,start_time,end_time,server_name,remark,tag_initmyprocess,
process_priority,next_time,create_date,create_time,edit_date,edit_time,class_name
from csc_quque where process_status in('T','R') and process_type != 'R'
--and start_time <= '2013-12-04 01:49:25'
order by start_time
--delete csc_quque where process_status in('T','R') and process_type != 'R'
--update csc_quque set process_status = 'A' where process_status in('T','R') and process_type != 'R' and start_time <= '2013-12-04 10:46:25'
select * from csc_quque where process_type = 'R' and process_status = 'T'
--update csc_quque set next_time = '2013-12-04 03:00:00'
where ququeid = 'YUMAUTO_HS'
select * from csc_quque where ququeid= '356840'
--update csc_quque set process_status = 'F' where ququeid = '356840'
select start_time,server_name,* from csc_quque where process_status = 'A'
/* server ö assing queue ŧ§ */
/* server ö assing queue ŧ§ */
/* process_normal */
with csc_server as (
select 'yum1' as 'server_name','4' as 'thread_process','10' as 'thread_workflow','10' as 'thread_report','active' as server_status,'2013-11-24 15:08:00' as 'last assign'
union all
select 'yum2' as 'server_name','4' as 'thread_process','10' as 'thread_workflow','10' as 'thread_report','active' as server_status,'2013-11-24 15:09:00' as 'last assign'
union all
select 'yum3' as 'server_name','6' as 'thread_process','10' as 'thread_workflow','10' as 'thread_report','active' as server_status,'2013-11-24 15:08:00' as 'last assign'
)
select top 1 s.server_name,s.server_status,s.thread_process,COUNT(q.server_name) as 'process_runing',s.[last assign]
from csc_server s LEFT OUTER JOIN CSC_QUQUE q
on s.server_name = q.SERVER_NAME and (q.PROCESS_STATUS = 'R' or q.PROCESS_STATUS = 'T') and q.PROCESS_GROUP = 'P' and PROCESS_TYPE != 'W'
where s.server_status = 'active'
group by s.server_name,s.thread_process,s.server_status,s.[last assign],s.thread_workflow,s.thread_report
having COUNT(q.server_name) < s.thread_process
order by process_runing,[last assign]
--checking sql upper
select * from csc_quque where SERVER_NAME = 'yum1'
/* server Шӹǹ еǶ */
select s.server_name,
s.thread_process,SUM(case when PROCESS_TYPE <> 'W' and PROCESS_GROUP = 'P' then 1 else 0 end) as process_normal,
s.thread_workflow,SUM(case when PROCESS_TYPE = 'W' and PROCESS_GROUP = 'P' then 1 else 0 end) as process_workflow,
s.thread_report,SUM(case when charindex(PROCESS_GROUP, 'RIEM') > 0 then 1 else 0 end) as process_report,
s.LAST_ACTION
from csc_server s left outer join CSC_QUQUE q
on s.server_status = 'active' and s.server_name = q.SERVER_NAME and (q.PROCESS_STATUS = 'R' or q.PROCESS_STATUS = 'T')
group by s.server_name,s.thread_process,s.thread_workflow,s.thread_report,s.LAST_ACTION
select * from csc_quque where QUQUEID between '1000' and '1003'
--update csc_quque set server_name = 'yum1',process_status = 'T',process_type='N',process_group = 'E' where QUQUEID between '1020' and '1021'
/* server ö assing queue ŧ§ */
/* condition */
select s.server_name,
s.thread_process,SUM(case when PROCESS_TYPE <> 'W' and PROCESS_GROUP = 'P' then 1 else 0 end) as process_normal,
s.thread_workflow,SUM(case when PROCESS_TYPE = 'W' and PROCESS_GROUP = 'P' then 1 else 0 end) as process_workflow,
s.thread_report,SUM(case when charindex(PROCESS_GROUP, 'RIEM') > 0 then 1 else 0 end) as process_report,
s.LAST_ACTION,s.LAST_ASSIGN
from csc_server s left outer join CSC_QUQUE q
on s.server_status = 'active' and s.server_name = q.SERVER_NAME and (q.PROCESS_STATUS = 'R' or q.PROCESS_STATUS = 'T')
group by s.server_name,s.thread_process,s.thread_workflow,s.thread_report,s.LAST_ACTION,s.LAST_ASSIGN
/* for process_normal */
--and process_normal < thread_process order by process_normal,LAST_ASSIGN
/* for process_workflow */
and process_workflow < thread_workflow order by process_workflow,LAST_ASSIGN
/* for process_report */
--and process_report < thread_workflow order by process_report,LAST_ASSIGN
IF OBJECT_ID ( 'time_in_zone', 'FN' ) IS NOT NULL
DROP function time_in_zone;
go
/* return 1 = true , 0 = false */
CREATE FUNCTION time_in_zone(@mytime datetime ,@beg_zone datetime , @endzone datetime)
RETURNS int
AS
BEGIN
IF @beg_zone <= @mytime and @mytime <= @endzone
BEGIN
return 1;
END
return 0;
END
go
IF OBJECT_ID ( 'getDiffIntersecTimeZone', 'FN' ) IS NOT NULL
DROP function getDiffIntersecTimeZone;
go
CREATE FUNCTION getDiffIntersecTimeZone(@in_beg_zone datetime , @out_beg_zone datetime , @in_end_zone datetime , @out_end_zone datetime)
returns varchar(7)
as
begin
declare @bgin int = 0;
declare @enin int = 0;
declare @bg datetime;
declare @en datetime;
declare @min int;
declare @hh int;
declare @mm int;
SET @bgin = dbo.time_in_zone(@in_end_zone , @in_beg_zone , @out_beg_zone);
SET @enin = dbo.time_in_zone(@out_end_zone , @in_beg_zone , @out_beg_zone);
IF @bgin = 1 and @enin = 1
BEGIN
SET @bg = @in_end_zone;
SET @en = @out_end_zone;
END
ELSE IF @bgin = 1
BEGIN
SET @bg = @in_end_zone;
SET @en = @out_beg_zone;
END
ELSE IF @enin = 1
BEGIN
SET @bg = @in_beg_zone;
SET @en = @out_end_zone;
END
ELSE
BEGIN
SET @bgin = dbo.time_in_zone(@in_beg_zone , @in_end_zone , @out_end_zone);
SET @enin = dbo.time_in_zone(@out_beg_zone , @in_end_zone , @out_end_zone);
IF @bgin = 1 and @enin = 1
BEGIN
SET @bg = @in_beg_zone;
SET @en = @out_beg_zone;
END
ELSE IF @bgin = 1
BEGIN
SET @bg = @in_beg_zone;
SET @en = @out_end_zone;
END
ELSE IF @enin = 1
BEGIN
SET @bg = @in_end_zone;
SET @en = @out_beg_zone;
END
ELSE
BEGIN
return '0.00';
END
END
SET @min = DATEDIFF(minute, @bg , @en);
SET @hh = @min / 60;
SET @mm = @min % 60;
IF @mm < 10
BEGIN
return cast(@hh as varchar(4))+'.0'+cast(@mm as varchar(2));
END
return cast(@hh as varchar(4))+'.'+cast(@mm as varchar(2));
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment