i'm running my application using one DB but during some specific table insertion i'm calling trigger where i need to get some value from some another DB.Previously i did this by hard coding the specific DB name inside the trigger. i got it correctly but now i need to remove the dependency of that DB name.so i need to find get that particular value in some another way. Can anyone help me to find the solution please...
Previous trigger.
CREATE TRIGGER [dbo].[xC2C_ADT_Upd] on [dbo].[dmAppt] For Insert,UPDATE
AS
BEGIN
If Not Exists (Select top 1* from inserted)
Return
declare @ADT_ID UniqueIdentifier
Declare @personuid int
declare @ApptID int
select @personuid = PID, @ApptID = IDAppt from inserted
set @ADT_ID = NewID()
declare @MSG_Type varchar(1)
declare @AddDate datetime
declare @LastUpdateDate datetime
declare @ServerID [varchar](50)
declare @ClientDB [varchar](max)
declare @ClientID [varchar](50)
set @ServerID = Convert(varchar(50),CONNECTIONPROPERTY('local_net_address'))
set @ClientDB = db_name()
select @ClientID = ClientID from dm_asarum_Client_Master..Clients //here only my database is hardcoded
where DBName_Current = @ClientDB
Now i did some changes,
set @ClientID = (select top 1 CAST(CONTEXT_INFO as varchar(50)) from sys.dm_exec_sessions where context_info!=0x order by last_request_end_time desc) --ClientID from dm_asarum_Client_Master..Clients
i have set the Context_info value in some SP like the following.,
alter PROCEDURE [dbo].[dm_ApptResRecurrReasonRelViewSel]
(
@IDLocation int,
@IDApptResource int,
@StartDate datetime,
@IDApptResRecurr int,
@ClientID varchar(50)
)
AS
SET NOCOUNT ON;
Declare @context_info1 varbinary(128);
SET
@context_info1 = CAST(@ClientID as varbinary(128));
SET
CONTEXT_INFO @context_info1;
Now the problem is,Using the SQL Profiler i noticed, i can able the get the clintID inside this SP correctly. but it is not saving this Context_info inside sys.dm_exec_sessions correctly. when i ran this manually in sql like,
exec [dm_ApptResRecurrReasonRelViewSel] @IDLocation=101,@IDApptResource=59,@StartDate='2016-05-27 17:00:00',@IDApptResRecurr=NULL,@ClientID='cl_obgyn_reg'
i context_info value is updated correctly i don't know where i missied.