Learn how to create replication objects and execute commands to implement an applied function.
Applied and request functions are very similar. The difference is that the maintenance user executes the applied function at the replicate site, and the same user who executes the stored procedure at the primary database executes the request function at the replicate site.
create proc update_pubs @pub_id char(4), @pub_name varchar(40), as update publishers set pub_name = @pub_name where pub_id = @pub_id
sp_setrepproc update_pubs, 'function'
create proc update_pubs pub_id char(4), @pub_name varchar(40), as update publishers set pub_name = @pub_name where pub_id = @pub_id
Do not mark this stored procedure as replicated. In applied function delivery, only the stored procedure in the primary database is marked as replicated.
However, if the replicate database modifies a standby database, mark the stored procedure in the active and standby replicate databases as replicated if you want to use stored procedure replication to the standby.
grant execute on update_pubs to maint_user
create applied function replication definition update_pubs_rep with primary at TOKYO_DS.pubs2 with all functions named update_pubs (@pub_id char(4), @pub_name varchar(40), @state char (2)) searchable parameters (@pub_name, @state)
The function replication definition must use the same parameter names and datatypes as the stored procedure in the primary database. You have the option to include only the parameters you want to replicate. If the function replication definition has 0 parameters, you must still include the parentheses for this clause.
If you specify searchable parameters, you can subscribe to function invocations based on the value of the function’s parameters. In the preceding example, @pub_name and @state are searchable parameters. Thus, for example, they can subscribe only to “CA” updates.
If you want to replicate the Adaptive Server timestamp datatype, declare the datatype binary(8) in the function replication definition.
See Replication Server Reference Manual > Replication Server Commands > create applied function replication definition.
If you are not using a default function-string class or a class inherited from the default or if you want to customize the function’s invocation, you need to create a function string for the user-defined function.
create subscription pubs_sub for update_pubs_rep with replicate at SYDNEY_DS.pubs2 where @state = 'CA' without materialization
If you specified searchable parameters in the function replication definition, you can subscribe to function invocations based on the value of the function’s parameters. In this example, the subscription only receives rows if the value of the @state parameter is equal to CA.
See Replication Server Reference Manual > Replication Server Commands > create subscription.
See Replication Server Reference Manual > RSSD Stored Procedures for information about stored procedures, such as rs_helpfunc, that you can use to query the RSSD for information about the replication system.