select * from [master].sys.assemblies
select * from [D10Task].sys.assemblies
--============================================================================
alter database [D10Task] set trustworthy on

if not exists (SELECT name FROM master.dbo.syslogins WHERE name = N'D10TaskOwner')
begin
    create login D10TaskOwner with password = 'sg4fdsviavFDxcCHFSD$@',cHECK_EXPIRATION = OFF, cHECK_POLICY =  OFF 
end

EXEC dbo.sp_changedbowner @loginame = N'D10TaskOwner', @map = false
go

use [master]
go
GRANT EXTERNAL ACCESS ASSEMBLY TO D10TaskOwner

use [D10Task]
go
create ASSEMBLY StoredProcedures
FROM 'C:\Program Files (x86)\TCSQL\StoredProcedures.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO

create ASSEMBLY [StoredProcedures.XmlSerializers]
FROM 'C:\Program Files (x86)\TCSQL\StoredProcedures.XmlSerializers.dll'
WITH PERMISSION_SET = SAFE;
GO

use [master]
go
if not exists  (select id from sysobjects where name = 'xp_smtp_sendmail')
begin
	exec sp_addextendedproc 'xp_smtp_sendmail', 'C:\Program Files (x86)\TCSQL\xpsmtp80.dll'
	grant execute on xp_smtp_sendmail to public
end
go
--========================================================================================
/* CLR sps */
use [D10Task]
go

CREATE PROCEDURE tc_ws_newTask
	@UserID int,
	@Task nvarchar(4000),
	@OrderedTime datetime = NULL,
	@Category int,
	@Comment nvarchar(4000) = '',
	@extParamStr nvarchar(max) = '',
	@perfID int = NULL,
	@priorityID int = 1,
	@D3TaskID int = 0,
	@ParentID int = 0,
	@UserNick nvarchar(20) = '',
	@remind bit = 0,
	@PropagateSubscribers bit = 1,
	@TaskStartTime datetime = NULL
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.NewTask
GO

CREATE PROCEDURE [dbo].[tc_ws_newTaskIgnoreError]
	@UserID [int],
	@Task [nvarchar](4000),
	@OrderedTime [datetime] = null,
	@Category [int],
	@Comment [nvarchar](4000) = '',
	@extParamStr [nvarchar](max) = '',
	@perfID [int] = null,
	@priorityID [int] = 1,
	@D3TaskID [int] = 0,
	@ParentID [int] = 0,
	@UserNick [nvarchar](20) = '',
	@remind [bit] = 0,
	@PropagateSubscribers [bit] = 1,
	@TaskStartTime datetime = NULL
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[NewTaskIgnoreErrors]
GO

CREATE PROCEDURE tc_ws_newTaskFromName
	@UserID int,
	@Task nvarchar(4000),
	@OrderedTime datetime = NULL,
	@Category int,
	@Comment nvarchar(4000) = '',
	@extParamStr nvarchar(max) = '',
	@perfID int = NULL,
	@priorityID int = 1,
	@D3TaskID int = 0,
	@ParentID int = 0,
	@UserNick nvarchar(20) = '',
	@remind bit = 0,
	@PropagateSubscribers bit = 1,
	@SessionUserId int = 0,
	@TaskStartTime datetime = NULL
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.NewTaskFromName
GO

CREATE PROCEDURE tc_ws_addComment
	@UserID int,
	@TaskID int,
	@Comment nvarchar(4000)
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddCommentSilent
GO

CREATE PROCEDURE tc_ws_updateOrderedTime
	@taskid int,
	@orderedTime datetime
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.UpdateOrderedTimeSilent
GO

CREATE PROCEDURE tc_ws_updateTaskDescription
	@TaskID int,
	@description nvarchar(4000)
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.UpdateTaskDescription
GO

CREATE PROCEDURE tc_ws_addNewPerformer
	@TaskID int,
	@UserID int,
	@SessionUserID int
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddPerformer
GO

CREATE PROCEDURE tc_ws_addSubscriber
	@UserID int,
	@TaskID int,
	@IsSubscribeChildTask bit = 0,
	@SubscribeType int = 5,
	@SessionUserID int
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddSubscriberSilent
GO

CREATE PROCEDURE tc_ws_changeTaskStep
	@StepID int,
	@TaskID int,
	@UserID int
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.ChangeTaskStep
GO

CREATE PROCEDURE tc_ws_updateExtParamsInTasks
	@TaskID int,
	@str nvarchar(max),
	@SessionUserID int
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.UpdateExtParamsInTasks
GO

CREATE PROCEDURE tc_ws_addAcceptant
	@TaskSignatureID int,
	@UserID int,
	@InitUserID int,
	@WriteComment bit = 1,
	@SendEmail bit = 1
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddAcceptant
GO

CREATE PROCEDURE dbo.tc_ws_AddNewSignatureToTask 
	@taskId int,
	@signatureId int,
	@UserID int,
	@isDynamic bit,
	@reason nvarchar(4000)
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddNewSignatureToTask
GO

CREATE PROCEDURE dbo.tc_ws_movetask 
	@UserID int,
	@taskId int,
	@subcatId int
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.MoveTask
GO

CREATE PROCEDURE dbo.tc_job_ws_PeriodicExecution
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.PeriodicExecution
GO

CREATE PROCEDURE tc_ws_addSubscriberMailed
	@UserID int,
	@TaskID int,
	@IsSubscribeChildTask bit = 0,
	@actingUserId INT
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddSubscriber
GO

CREATE PROCEDURE tc_ws_AddUserComment
	@taskId int,
	@commentRecipients nvarchar(4000),
	@userId int,
	@text nvarchar(4000),
	@needSubscribe bit = 1
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddUserComment
GO

CREATE PROCEDURE tc_ws_AddUserCommentWithSms
	@taskId int,
	@commentRecipients nvarchar(4000),
	@userId int,
	@text nvarchar(4000),
	@needSubscribe bit = 1
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.AddUserCommentWithSms
GO

CREATE PROCEDURE tc_ws_FindSidForNick
	@nick nvarchar(50),
	@sid nvarchar(4000) output
AS
EXTERNAL NAME StoredProcedures.StoredProcedures.FindSidForNick
GO

CREATE PROCEDURE [dbo].[tc_ws_DeleteRequestedSignature]
	@UserID [int],
	@TaskSignatureID [int]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[DeleteRequestedSignature]
GO

CREATE PROCEDURE [dbo].[tc_ws_sub_InsertDatesIntoCalendar]
	@taskId [int]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[InsertDatesIntoCalendar]
GO

CREATE PROCEDURE tc_ws_DeleteTask
	@taskId int,
	@userId int
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[DeleteTask]
GO

CREATE PROCEDURE tc_ws_UpdateTaskStartTime
	@taskId int,
	@startTime DateTime,
	@userId int,
	@reason nvarchar(4000)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[UpdateTaskStartTime]
GO

CREATE PROCEDURE tc_ws_SendSmsToPhone
	@number nvarchar(4000),
	@text nvarchar(4000),
	@taskID int = null
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[SendSmsToPhone]
GO

CREATE PROCEDURE tc_ws_SendSmsToUser
	@userID int,
	@text nvarchar(4000),
	@taskID int = null
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[SendSmsToUser]
GO

CREATE PROCEDURE tc_ws_SendSmsToPhoneList
	@numbers nvarchar(4000),
	@text nvarchar(4000),
	@taskID int = null
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[SendSmsToPhoneList]
GO

CREATE PROCEDURE tc_ws_SendSmsToGroup
	@groupID int,
	@text nvarchar(4000),
	@taskID int = null
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].[SendSmsToGroup]
GO

CREATE PROCEDURE tc_ws_AddResponsiblePerformer
@taskID int,
@sessionUserID int,
@userToAddId int
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [StoredProcedures].[StoredProcedures].AddResponsiblePerformer