Replication when changing the subscription field - replication

I'm tring to understand a problem we have on replication but I cannot find a definitive explaination when it comes to the subscription.
Lets say I have a table, 'T' [label, name] and we create a subscription for replication, " where label = 'XX' "
Now I insert into T values ('XX', 'exes') and this will replicate to my other database.
Then if I run; update T set label = 'YY' where label = 'XX'
Will the update be replicated because the orginal record's label matches the subscription? Or will it be filtered from replication because the new label does not match the subscription?
Thanks

Related

Generate code for specific record instead of all records

I have code to generate a unique code for a filed in a SQL Server 2017 table. The entire code block works great without any issues. Due to user requirements I have to do this differently now. As you can see from my code it will generate a code for each row of data and set all rows 'Valid' filed to valid. What I absolutely have to do is generate the code for a specific record. And specifically for the most recently updated record. I totally understand how to get the most recently updated records and how to apply IDENT_CURRENT, ##IDENTITY, and SCOPE_IDENTITY but I wondered if there is something similar for recently Updated records. When I say updated record an Approver in Finance will change the Finance_Approval field status 'approved'. That specific record that was just changed to 'approved' is the one I want to generate my code againt. This code:
UPDATE Req_submitted
Set approved_code =
(SELECT FLOOR(RAND()*(525885-15+9)+16458)),
Valid = 'valid'
Below is all of code for the Trigger. I'm hoping this is a challenge someone wants to take on as my SQL has hit a wall on this one but the feature is very important.
ALTER TRIGGER [dbo].[Approve_request_trig]
ON [dbo].[Req_submitted]
AFTER update
AS
Begin
declare
#req_submitted_key int,
#Submitted_to_finance_approver_email varchar(50),
#approved_denied varchar(50),
#approved_finance varchar(50)
select #req_submitted_key = s.req_submitted_key,
#Submitted_to_finance_approver_email = s.Submitted_to_finance_approver_email,
#approved_denied = s.approved_denied,
#approved_finance = s.approved_finance
from inserted s;
if update(approved_finance)
UPDATE Req_submitted
Set approved_code =
(SELECT FLOOR(RAND()*(525885-15+9)+16458)),
Valid = 'valid'
;
End

Automatically fill row with value based on inserted id

I have a table where the user is able to insert the ID of a Node that corresponds to a title elsewhere in the database. I want this tile to be automatically inserted into the row after the user has chosen the id.
This is my table:
I need to have the "SommerhusNavn" column be automatically filled with values based on the "SommerhusId" inserted.
I am using a third party to handle the CRUD functionality, where the user picks the ID from a dropdown. I already know in which table the title for the ID is located, I'm just not sure how to fill the row with the insert statement. Would I need to run a separate query for this to happen?
Edit:Solution
CREATE TRIGGER [dbo].[BlokeredePerioderInsert]
ON dbo.BlokeredePerioder
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE BlokeredePerioder SET SommerhusNavn = text FROM umbracoNode AS umbNode
where SommerhusId = umbNode.id
END
GO
Yes, you need to run additional UPDATE query. Let's assume that you have the TitlesTable, with columns ID and Title. Then it should look like:
UPDATE MyTable SET SommerhusNavn = Title FROM TitlesTable AS A
WHERE SommerhusId = A.ID
AND SommerhusNavn IS NOT NULL --not necessary
Perhaps i'm not understanding, but why can't you use send the value across in the initial update?
Can you use a trigger on the database side?
Alternatively, you'll need to send a update across, following the insert.

Data manipulation logic !! should be as a part of fetch via DB link or an independent routine

The question may not be sufficient to give an insight.
I have two DB instances : A and B on the same server.
B reads data from several tables in A(A1,A2,A3...) via DB link and maintains history of data in the replicated tables(A1_ext, A2_ext, A3_ext, as they have additional columns assume its status column) , i.e if its identified that a new row has been added in A1 ,a row is created in A1_ext with some status called as VALID , if a row is updated in A1, the existing data in A1_ext is updated to INVALID and a new row having latest data from A1 is created in A1_ext with status VALID.
For now the implemenetd logic is : Read data from A1 via db link , check if its exists in A1_ext , if does, delimit the existing one and create a new one.
Is it an efficient approach??
Or should it be like read all updated data from A1 and pull them at one go(bulk collect say) on B instance in A1_stag table(new). Then run the logic of update/insert on A1_ext.
The best I can come up with is something like the following:
-- Insert the new and changed records with status NEW
insert into A1_ext
with upsert as (
select id, val from A1#RemoteDB
minus
select id, val from A1_ext where status = 'VALID'
) select id, val, 'NEW' from upsert;
-- Update the old VALID records that have NEW records to INVALID
update A1_ext old
set status = 'INVALID'
where status = 'VALID'
and exists (select 1 from A1_ext new
where new.id = old.id
and new.status = 'NEW');
-- Update all NEW records to VALID
update A1_ext set status = 'VALID' where status = 'NEW';
Unfortunately the first query is going to do a full table scan on A1#RemoteDB and transmit all that data across the database link. Possibly not a big deal when both DBs reside on the same server, but possibly a performance problem for large tables across a network. The minus operation will prune away the unchanged records after they've crossed the link but before they get into the *_EXT table. If you can reliably filter the source data to just the new and updated records that would help limit the amount of data crossing the DB link.
The 2nd and 3rd queries are just house keeping to mark updated records as invalid and to mark the new data as valid.
If possible keep this as pure SQL and avoid context switching between SQL and PL/SQL as much as possible.

MSSQL 2008 Update record if column matches value from same column in another record

First time asking a question here. To give a little background we have a database that contains some configuration information for PCs. These configurations have a field for the serial number in them as well as a field for a contact (who uses the PC). The contact field was always manually set and now we have moved to a new naming scheme which means that all the configuration records have been recreated with new names. We need to update the new records being created with the contact value from the old record, all the records are in the same table called 'Config'
Example;
OLD: machineid.location
NEW: location.machineid
I have been able to write a SELECT query to select the records that match serial numbers however I'm having a hard time turning it into an UPDATE query so that the 'Contact' field on the new record can be set with the 'Contact' field from the old record. The current SELECT statement I have is below;
SELECT *
FROM Config ta1
JOIN Config ta2 on ta1.Config_ID != ta2.Config_ID
WHERE ta1.Serial_Number = ta2.Serial_Number AND
(ta1.Serial_Number is not null OR ta1.Serial_Number = ' ') AND
ta1.Config_Name != ta2.Config_Name
Any help would be greatly appreciated, I have been digging around on here but have not had too much luck.
ta1 is the new record and ta2 is the old one, correct? And the name of the column to update is Contact? This should work then:
UPDATE Config
SET Contact = ta2.Contact
FROM Config ta1
JOIN Config ta2 on ta1.Config_ID != ta2.Config_ID
WHERE ta1.Serial_Number = ta2.Serial_Number AND
(ta1.Serial_Number is not null OR ta1.Serial_Number = ' ') AND
ta1.Config_Name != ta2.Config_Name
Probably want to wrap it in a BEGIN/ROLLBACK structure and add a SELECT after the UPDATE in the transaction it to make sure you're getting what you want, but that should do it.

SQL Server Query Command

I have an SQL database table, I want to find everything in a table where the 'Room' says 'DISPOSED', Insert 'DISPOSED' into the 'Status' field and then delete the entry in 'Room'.
Basically moving the entry from one field to another (if the 'Room' field has 'DISPOSED' in it)
Hope this makes sense.
Thanks for any help.
Update table_name SET room='', Stauts='DISPOSED' where Room='DISPOSED'
OR
Update table_name SET room=null, Stauts='DISPOSED' where Room='DISPOSED'
The SQL is
UPDATE table
SET status = room, room = null
WHERE room = 'DISPOSED'