Showing posts with label version. Show all posts
Showing posts with label version. Show all posts

Monday, March 26, 2012

Merge Replication using a guid as a dynamic filter

Hi ...

I am working on a project where the server version of application has vouchers from different entities. I have created a publication manually. My next step was to create a client subscription using rmo and to execute a pull. This part works fine. Code samples from http://msdn2.microsoft.com/en-us/library/ms147314.aspx

My next step would be to implement dynamic filtering using the guid of the entity as a parameter.

I dont want to use suser_sname() or host_name() as I want to use a fixed login for the replication for all users, and a client could have several host dbs (sql express, sql mobile)

My goal would be to pass a guid-value to the HostName Property of the MergePullSubscription class and convert it to an uniquidentifier and use it as a filter as I have not found any other way to pass a guid as a filter.

RMO-Code:

subscription.HostName = "4bb0e468-c68a-4253-ba82-f71c3a6e302d"

Filter:

SELECT <published_columns> FROM [dbo].[voucher] WHERE [entity_ID] = dbo.fx_ConvertHostToEntity()

Function:

create function fx_ConvertHostToEntity()
returns uniqueidentifier
as
Begin
declare @.host nvarchar(50)
set @.host = host_name()
declare @.entity uniqueidentifier
set @.entity = cast( @.host as uniqueidentifier)
return @.entity
End

When trying to set the filter sql server complains that a character string cannot be casted to a uniqueidentifier - so i can not set this filter. Is there a way to pass a parameter other then the username or the hostname as a filter?

SELECT <published_columns> FROM [dbo].[voucher] WHERE [entity_ID] =@.entity, where @.entity is a guid

Thanks for your support

Alex

Hi, Alex,

You can convert the string to binary then to uniqueidentifier, like following:

declare @.host nvarchar(50)
set @.host = host_name()
declare @.entity uniqueidentifier
set @.entity = cast( convert(varbinary, @.host) as uniqueidentifier)
print @.entity

Output:


00450044-004C-004C-3000-340032003700

Thanks,

Zhiqiang Feng

|||

Thanks for the info changed the function according to your suggestion.

Now a new problem arrises: I receive 0 rows for the filtered tables. When changing the funktion to

ALTER FUNCTION [dbo].[fx_ConvertHostToEntity] ()
returns uniqueidentifier
as
Begin
declare @.host nvarchar(50)
set @.host = host_name()
declare @.entity uniqueidentifier
set @.entity = cast( convert(varbinary, @.host) as uniqueidentifier)
-- return @.entity
return '4bb0e468-c68a-4253-ba82-f71c3a6e302d'
End

I get the rows in the replicated table. So the problem must be somewhere either in the creation of the sanpshots or the resolution of the hostname

|||

Using Replication Monitor for the publication I also found out using the properties of the publication that the snapshot for the data partition has not been created. So I did this manually and will code it later on. When replicating again I got the follwowing error shown up in the Replication Monitor (I left out the first one as I am not using web sync right now):

Partitioned snapshot validation failed for this Subscriber. The snapshot validation token stored in the specified partitioned snapshot location does not match the value '{00620034-0062-0030-6500-340036003800}' used by the Merge Agent when evaluating the parameterized filter function. If specifying the location of the partitioned snapshot (using -DynamicSnapshotLocation), you must ensure that the snapshot files in that directory belong to the correct partition or allow the Merge Agent to automatically detec (Source: MSSQL_REPL, Error number: MSSQL_REPL27223)

Find the full source code below:

Imports Microsoft.SqlServer.Replication

Imports Microsoft.SqlServer.Management.Common

Public Class Replication

Private subscriberName As String

Private publisherName As String

Private windowsLogin As String

Private windowsPWD As String

Private publicationName As String

Private publicationDbName As String

Private subscriptionDbName As String

Private passedHostname As String

''' <summary>

''' a new instance of the replication object

''' </summary>

''' <param name="EntityID">id of the entity as string</param>

''' <param name="SubscriberHost">the hostname of the subscribing sql instance</param>

''' <param name="PublisherHost">the hostname of the publishing sql instance</param>

''' <param name="Login">the name of the windows login to authenticate towards the publisher: domain\user</param>

''' <param name="PWD">the password</param>

''' <param name="Publication">the name of the puplicaiton on the publisher</param>

''' <param name="PublicationDB">the name of the puplication db</param>

''' <param name="SubscriptionDB">the name of the subscription db</param>

''' <remarks></remarks>

Sub New(ByVal EntityID As Guid, ByVal SubscriberHost As String, ByVal PublisherHost As String, ByVal Login As String, ByVal PWD As String, ByVal Publication As String, ByVal PublicationDB As String, ByVal SubscriptionDB As String)

subscriberName = SubscriberHost

publisherName = PublisherHost

'the guid of the entity is passed as hostname to be used for filtering

PassedHostname = EntityID.ToString

windowsLogin = Login

windowsPWD = PWD

publicationName = Publication

subscriptionDbName = SubscriptionDB

publicationDbName = PublicationDB

End Sub

Sub SetupPullSubscription()

'Create connections to the Publisher and Subscriber.

Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)

Dim publisherConn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.

Dim publication As MergePublication

Dim subscription As MergePullSubscription

Try

' Connect to the Subscriber.

subscriberConn.Connect()

' Ensure that the publication exists and that

' it supports pull subscriptions.

publication = New MergePublication()

publication.Name = publicationName

publication.DatabaseName = publicationDbName

publication.ConnectionContext = publisherConn

If publication.LoadProperties() Then

If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then

publication.Attributes = publication.Attributes Or PublicationAttributes.AllowPull

End If

' Define the pull subscription.

subscription = New MergePullSubscription()

subscription.ConnectionContext = subscriberConn

subscription.PublisherName = publisherName

subscription.PublicationName = publicationName

subscription.PublicationDBName = publicationDbName

subscription.DatabaseName = subscriptionDbName

subscription.HostName = passedHostname

' Specify the Windows login credentials for the Merge Agent job.

subscription.SynchronizationAgentProcessSecurity.Login = windowsLogin

subscription.SynchronizationAgentProcessSecurity.Password = windowsPWD

' Make sure that the agent job for the subscription is created.

subscription.CreateSyncAgentByDefault = True

' Create the pull subscription at the Subscriber.

subscription.Create()

Dim registered As Boolean = False

' Verify that the subscription is not already registered.

For Each existing As MergeSubscription In _

publication.EnumSubscriptions()

If existing.SubscriberName = subscriberName Then

registered = True

End If

Next

If Not registered Then

' Register the local subscription with the Publisher.

publication.MakePullSubscriptionWellKnown(subscriberName, subscriptionDbName, SubscriptionSyncType.Automatic, MergeSubscriberType.Local, 0)

'publication.MakePullSubscriptionWellKnown(subscriberName, subscriptionDbName, SubscriptionSyncType.Automatic, MergeSubscriberType.Local, 0)

End If

Else

' Do something here if the publication does not exist.

Throw New ApplicationException(String.Format("The publication '{0}' does not exist on {1}.", publicationName, publisherName))

End If

Catch ex As Exception

' Implement the appropriate error handling here.

Throw New ApplicationException(String.Format("The subscription to {0} could not be created.", publicationName), ex)

Finally

subscriberConn.Disconnect()

publisherConn.Disconnect()

End Try

End Sub

Sub PullMergeReplication()

' Create a connection to the Subscriber.

Dim conn As ServerConnection = New ServerConnection(subscriberName)

Dim subscription As MergePullSubscription

Try

' Connect to the Subscriber.

conn.Connect()

' Define subscription properties.

subscription = New MergePullSubscription()

subscription.ConnectionContext = conn

subscription.DatabaseName = subscriptionDbName

subscription.PublisherName = publisherName

subscription.PublicationDBName = publicationDbName

subscription.PublicationName = publicationName

' If the pull subscription and the job exists, start the agent job.

If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then

subscription.SynchronizeWithJob()

Else

' Do something here if the subscription does not exist.

Throw New ApplicationException(String.Format("A subscription to '{0}' does not exists on {1}", publicationName, subscriberName))

End If

Catch ex As Exception

' Do appropriate error handling here.

Throw New ApplicationException("The subscription could not be synchronized.", ex)

Finally

conn.Disconnect()

End Try

End Sub

End Class

|||

this means you're trying to apply a dynamic snapshot that's not for your partition. i.e. your filter is for 'StoreA', but you're trying to apply snapshot belonging to 'StoreB'.

|||

After finding no solution to the problem, feeling that the problem has something to to with the conversion vom guid to string, I decided to have a second col in the table that was filled with a trigger: the guid converted to nvarchar.

And suddenly I knew where the problem was hiding:

hostname value: 4bb0e468-c68a-4253-ba82-f71c3a6e302d

cast( convert(varbinary,host_name()) as uniqueidentifier) -> 4BB0E468-C68A-4253-BA82-F71C3A6E302D

And that is the solution: When using a guid as the hostname and filter, the values have to be either both lower case or upper case. Otherwise you will get an empty result for dynamicly filtered tables :)

Thanks for your support

Alex

Friday, March 9, 2012

Merge replication in 2005... Version conflict?

I recently Upgraded both of my servers to SQL Server 2005 Standard.
Upon trying to set up Merge replication between the 2, I get this error:
For merge publications, the version of the Subscriber must not exceed the
version of the Publisher. (New Subscription Wizard)
They both have 2005 installed and updated (9.00.1399.00)
They both have the latest MDAC and .net versions
What could be causing this error?
Thanks!
what are the respective OSs? Could you post your publication script here?
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Ben" <Ben@.discussions.microsoft.com> wrote in message
news:CA2A8A38-E07F-4C16-9C05-F58FA92D61F2@.microsoft.com...
>I recently Upgraded both of my servers to SQL Server 2005 Standard.
> Upon trying to set up Merge replication between the 2, I get this error:
> For merge publications, the version of the Subscriber must not exceed the
> version of the Publisher. (New Subscription Wizard)
> They both have 2005 installed and updated (9.00.1399.00)
> They both have the latest MDAC and .net versions
> What could be causing this error?
> Thanks!
|||They are both Running Windows 2000 server.
Here is the script:
-- Enabling the replication database
use master
exec sp_replicationdboption @.dbname = N'twistpos', @.optname = N'merge
publish', @.value = N'true'
GO
-- Adding the merge publication
use [twistpos]
exec sp_addmergepublication @.publication = N'AccGC', @.description = N'Merge
publication of database ''twistpos'' from Publisher ''SERVER''.', @.sync_mode
= N'native', @.retention = 14, @.allow_push = N'true', @.allow_pull = N'true',
@.allow_anonymous = N'true', @.enabled_for_internet = N'false',
@.snapshot_in_defaultfolder = N'true', @.compress_snapshot = N'false',
@.ftp_port = 21, @.ftp_login = N'anonymous', @.allow_subscription_copy =
N'false', @.add_to_active_directory = N'false', @.centralized_conflicts =
N'true', @.dynamic_filters = N'false', @.conflict_retention = 14,
@.keep_partition_changes = N'true', @.allow_synctoalternate = N'false',
@.max_concurrent_merge = 0, @.max_concurrent_dynamic_snapshots = 0
GO
exec sp_addpublication_snapshot @.publication = N'AccGC', @.frequency_type =
4, @.frequency_interval = 14, @.frequency_relative_interval = 1,
@.frequency_recurrence_factor = 0, @.frequency_subday = 1,
@.frequency_subday_interval = 5, @.active_start_time_of_day = 500,
@.active_end_time_of_day = 235959, @.active_start_date = 0, @.active_end_date =
0, @.snapshot_job_name = N'SERVER-twistpos-AccGC-6'
exec sp_grant_publication_access @.publication = N'AccGC', @.login =
N'BUILTIN\Administrators'
GO
exec sp_grant_publication_access @.publication = N'AccGC', @.login =
N'distributor_admin'
GO
exec sp_grant_publication_access @.publication = N'AccGC', @.login =
N'DOMAIN\Administrator'
GO
exec sp_grant_publication_access @.publication = N'AccGC', @.login = N'sa'
GO
-- Adding the merge articles
use [twistpos]
exec sp_addmergearticle @.publication = N'AccGC', @.article = N'GiftCards',
@.source_owner = N'dbo', @.source_object = N'GiftCards', @.type = N'table',
@.description = N'', @.creation_script = N'', @.pre_creation_cmd = N'drop',
@.schema_option = 0x0000000000004FF1, @.auto_identity_range = N'true',
@.pub_identity_range = 100000, @.identity_range = 100000, @.threshold = 90,
@.destination_owner = N'dbo', @.column_tracking = N'false',
@.subset_filterclause = N'', @.vertical_partition = N'false',
@.verify_resolver_signature = 1, @.allow_interactive_resolver = N'false',
@.fast_multicol_updateproc = N'true', @.check_permissions = 0
GO
use [twistpos]
exec sp_addmergearticle @.publication = N'AccGC', @.article = N'Account',
@.source_owner = N'dbo', @.source_object = N'Account', @.type = N'table',
@.description = N'', @.creation_script = N'', @.pre_creation_cmd = N'drop',
@.schema_option = 0x0000000000006FF1, @.auto_identity_range = N'false',
@.destination_owner = N'dbo', @.column_tracking = N'false',
@.subset_filterclause = N'', @.vertical_partition = N'false',
@.verify_resolver_signature = 1, @.allow_interactive_resolver = N'false',
@.fast_multicol_updateproc = N'true', @.check_permissions = 0
GO
"Hilary Cotter" wrote:

> what are the respective OSs? Could you post your publication script here?
> --
> Hilary Cotter
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
> "Ben" <Ben@.discussions.microsoft.com> wrote in message
> news:CA2A8A38-E07F-4C16-9C05-F58FA92D61F2@.microsoft.com...
>
>
|||see this command? delete it,
- it works then - publication_compatibility_level = N'80RTM',
unless your subscriber really is SQL 2000 RTM. I would also advise you to
drop the existing subscription database and recreate it.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Ben" <Ben@.discussions.microsoft.com> wrote in message
news:38EC9C88-370F-49D2-9257-D434E8BC6C30@.microsoft.com...[vbcol=seagreen]
> They are both Running Windows 2000 server.
> Here is the script:
> -- Enabling the replication database
> use master
> exec sp_replicationdboption @.dbname = N'twistpos', @.optname = N'merge
> publish', @.value = N'true'
> GO
> -- Adding the merge publication
> use [twistpos]
> exec sp_addmergepublication @.publication = N'AccGC', @.description =
> N'Merge
> publication of database ''twistpos'' from Publisher ''SERVER''.',
> @.sync_mode
> = N'native', @.retention = 14, @.allow_push = N'true', @.allow_pull =
> N'true',
> @.allow_anonymous = N'true', @.enabled_for_internet = N'false',
> @.snapshot_in_defaultfolder = N'true', @.compress_snapshot = N'false',
> @.ftp_port = 21, @.ftp_login = N'anonymous', @.allow_subscription_copy =
> N'false', @.add_to_active_directory = N'false', @.centralized_conflicts =
> N'true', @.dynamic_filters = N'false', @.conflict_retention = 14,
> @.keep_partition_changes = N'true', @.allow_synctoalternate = N'false',
> @.max_concurrent_merge = 0, @.max_concurrent_dynamic_snapshots = 0
> GO
>
> exec sp_addpublication_snapshot @.publication = N'AccGC', @.frequency_type =
> 4, @.frequency_interval = 14, @.frequency_relative_interval = 1,
> @.frequency_recurrence_factor = 0, @.frequency_subday = 1,
> @.frequency_subday_interval = 5, @.active_start_time_of_day = 500,
> @.active_end_time_of_day = 235959, @.active_start_date = 0, @.active_end_date
> =
> 0, @.snapshot_job_name = N'SERVER-twistpos-AccGC-6'
> exec sp_grant_publication_access @.publication = N'AccGC', @.login =
> N'BUILTIN\Administrators'
> GO
> exec sp_grant_publication_access @.publication = N'AccGC', @.login =
> N'distributor_admin'
> GO
> exec sp_grant_publication_access @.publication = N'AccGC', @.login =
> N'DOMAIN\Administrator'
> GO
> exec sp_grant_publication_access @.publication = N'AccGC', @.login = N'sa'
> GO
> -- Adding the merge articles
> use [twistpos]
> exec sp_addmergearticle @.publication = N'AccGC', @.article = N'GiftCards',
> @.source_owner = N'dbo', @.source_object = N'GiftCards', @.type = N'table',
> @.description = N'', @.creation_script = N'', @.pre_creation_cmd = N'drop',
> @.schema_option = 0x0000000000004FF1, @.auto_identity_range = N'true',
> @.pub_identity_range = 100000, @.identity_range = 100000, @.threshold = 90,
> @.destination_owner = N'dbo', @.column_tracking = N'false',
> @.subset_filterclause = N'', @.vertical_partition = N'false',
> @.verify_resolver_signature = 1, @.allow_interactive_resolver = N'false',
> @.fast_multicol_updateproc = N'true', @.check_permissions = 0
> GO
> use [twistpos]
> exec sp_addmergearticle @.publication = N'AccGC', @.article = N'Account',
> @.source_owner = N'dbo', @.source_object = N'Account', @.type = N'table',
> @.description = N'', @.creation_script = N'', @.pre_creation_cmd = N'drop',
> @.schema_option = 0x0000000000006FF1, @.auto_identity_range = N'false',
> @.destination_owner = N'dbo', @.column_tracking = N'false',
> @.subset_filterclause = N'', @.vertical_partition = N'false',
> @.verify_resolver_signature = 1, @.allow_interactive_resolver = N'false',
> @.fast_multicol_updateproc = N'true', @.check_permissions = 0
> GO
> "Hilary Cotter" wrote:
|||You still have it set in 80 compatibility mode.
Mike
http://www.solidqualitylearning.com
Disclaimer: This communication is an original work and represents my sole
views on the subject. It does not represent the views of any other person
or entity either by inference or direct reference.
"Ben" <Ben@.discussions.microsoft.com> wrote in message
news:CA2A8A38-E07F-4C16-9C05-F58FA92D61F2@.microsoft.com...
>I recently Upgraded both of my servers to SQL Server 2005 Standard.
> Upon trying to set up Merge replication between the 2, I get this error:
> For merge publications, the version of the Subscriber must not exceed the
> version of the Publisher. (New Subscription Wizard)
> They both have 2005 installed and updated (9.00.1399.00)
> They both have the latest MDAC and .net versions
> What could be causing this error?
> Thanks!
|||OK... So here's what I figured out.
In Object Explorer, I see my subscription server as version 9.0.1399, but
the publication server is still marked 8.0.194.
How do I upgrade the publication server to 9.0.1399?
Do I still need to drop the old merge replication? How do I go about that?
Thanks for the help... still new to this 2005 thing.
"Hilary Cotter" wrote:

> see this command? delete it,
> - it works then - publication_compatibility_level = N'80RTM',
> unless your subscriber really is SQL 2000 RTM. I would also advise you to
> drop the existing subscription database and recreate it.
>
> --
> Hilary Cotter
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
> "Ben" <Ben@.discussions.microsoft.com> wrote in message
> news:38EC9C88-370F-49D2-9257-D434E8BC6C30@.microsoft.com...
>
>
|||OK, I'm really confused here. Can you issue a select @.@.version on your
publisher and post it here marked publisher, and do the same on your
subscriber and post it here marked subscriber.
You can't create SQL 2005 publications on SQL 2000 publishers - there is
something very wrong here. Besides I was able to repro your problem between
two SQL 2005 machines.
To drop the existing merge publications, right click on it in Enterprise
Manager or SQL Server Management Studio. and select Delete.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Ben" <Ben@.discussions.microsoft.com> wrote in message
news:3B87E513-4C0E-4294-B45D-273485DDD94F@.microsoft.com...[vbcol=seagreen]
> OK... So here's what I figured out.
> In Object Explorer, I see my subscription server as version 9.0.1399, but
> the publication server is still marked 8.0.194.
> How do I upgrade the publication server to 9.0.1399?
> Do I still need to drop the old merge replication? How do I go about that?
> Thanks for the help... still new to this 2005 thing.
> "Hilary Cotter" wrote:
|||Just change the database compatibility level within the database properties.
Mike
http://www.solidqualitylearning.com
Disclaimer: This communication is an original work and represents my sole
views on the subject. It does not represent the views of any other person
or entity either by inference or direct reference.
"Ben" <Ben@.discussions.microsoft.com> wrote in message
news:3B87E513-4C0E-4294-B45D-273485DDD94F@.microsoft.com...[vbcol=seagreen]
> OK... So here's what I figured out.
> In Object Explorer, I see my subscription server as version 9.0.1399, but
> the publication server is still marked 8.0.194.
> How do I upgrade the publication server to 9.0.1399?
> Do I still need to drop the old merge replication? How do I go about that?
> Thanks for the help... still new to this 2005 thing.
> "Hilary Cotter" wrote:
|||The instance had not been updated. Reattatched and everything was fine.
Thanks for the help.
Ben
"Michael Hotek" wrote:

> Just change the database compatibility level within the database properties.
> --
> Mike
> http://www.solidqualitylearning.com
> Disclaimer: This communication is an original work and represents my sole
> views on the subject. It does not represent the views of any other person
> or entity either by inference or direct reference.
> "Ben" <Ben@.discussions.microsoft.com> wrote in message
> news:3B87E513-4C0E-4294-B45D-273485DDD94F@.microsoft.com...
>
>