复制自定义解析器将空字符串更改为 NULL

发布于 2024-08-07 02:24:36 字数 1265 浏览 4 评论 0原文

我们有一个 C# 应用程序,它发送到一个数据库,该数据库被复制到另一个数据库(使用合并复制),并且有一个自定义解析器,它是一个存储过程。

这在 SQL Server 2000 下工作正常,但在 SQL Server 2005 下测试时,自定义解析器尝试将任何空 varchar 列更改为空(并且失败,因为该特定列不允许空)。

请注意,这些 varchar 字段不是导致冲突的字段,因为它们在两个数据库上当前都是空的,并且没有被更改,并且存储过程不会更改它们(它所做的只是尝试设置另一个 Money 列的值) 。

有没有人遇到过这个问题,或者有一个存储过程的示例,它将保留空字符串?

实际的存储过程相当简单,并且在发生冲突时重新计算客户余额。

ALTER procedure [dbo].[ReCalculateCustomerBalance]
    @tableowner sysname,
    @tablename sysname,
    @rowguid varchar(36),
    @subscriber sysname,
    @subscriber_db sysname,
    @log_conflict INT OUTPUT,
    @conflict_message nvarchar(512) OUTPUT
AS
    set nocount on
DECLARE
    @CustomerID  bigint,
    @SysBalance money,
    @CurBalance money,
    @SQL_TEXT nvarchar(2000)

    Select @CustomerID = customer.id from customer where rowguid=  @rowguid

    Select @SysBalance = Sum(SystemTotal), @CurBalance = Sum(CurrencyTotal)  From CustomerTransaction Where CustomerTransaction.CustomerID = @CustomerID

    Update Customer Set SystemBalance = IsNull(@SysBalance, 0), CurrencyBalance = IsNull(@CurBalance, 0) Where id = @CustomerID

    Select * From Customer Where rowguid= @rowguid

    Select @log_conflict =0
    Select @conflict_message ='successful'
    Return(0)

We have an C# application which posts to a database which is replicated to another database (using merge-replication) and has one custom resolver which is a stored procedure.

This was working fine under SQL Server 2000 , but when testing under SQL Server 2005 the custom resolver is attempting to change any empty varchar columns to be nulls (and failing cos this particular column does not allow nulls).

Note that these varchar fields are not the ones which cause the conflict as they are current empty on both databases and are not being changed and the stored procedure does not change them (all it is doing is attempting to set the value of another money column).

Has anyone come across this problem, or has example of a stored procedure which will leave empty strings as they are?

The actual stored procedure is fairly simply and and re-calculates the customer balance in the event of a conflict.

ALTER procedure [dbo].[ReCalculateCustomerBalance]
    @tableowner sysname,
    @tablename sysname,
    @rowguid varchar(36),
    @subscriber sysname,
    @subscriber_db sysname,
    @log_conflict INT OUTPUT,
    @conflict_message nvarchar(512) OUTPUT
AS
    set nocount on
DECLARE
    @CustomerID  bigint,
    @SysBalance money,
    @CurBalance money,
    @SQL_TEXT nvarchar(2000)

    Select @CustomerID = customer.id from customer where rowguid=  @rowguid

    Select @SysBalance = Sum(SystemTotal), @CurBalance = Sum(CurrencyTotal)  From CustomerTransaction Where CustomerTransaction.CustomerID = @CustomerID

    Update Customer Set SystemBalance = IsNull(@SysBalance, 0), CurrencyBalance = IsNull(@CurBalance, 0) Where id = @CustomerID

    Select * From Customer Where rowguid= @rowguid

    Select @log_conflict =0
    Select @conflict_message ='successful'
    Return(0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-08-14 02:24:36

这里有几个选项,每个选项都是我的研究显示的 SQL Server 问题的一些解决方法。

1- 更改此语句:Select * From CustomerWhere rowguid= @rowguid 明确提及每一列,并对有问题的字段使用“isNull”

2- 更改表中的列以添加'' 的默认约束。这样做的作用是,如果您尝试插入“null”,它将用空字符串替换它

3-添加一个“插入之前”触发器,该触发器将在插入之前更改数据,以不再包含“null”

PS:您确定复制系统将该列标记为“必需”吗?我认为如果不需要的话,如果没有数据存在,它会插入“null”。

You have a few options here, each are a bit of a workaround from what my research seems to show is an issue with SQL Server.

1- Alter this statement: Select * From Customer Where rowguid= @rowguid to explicitly mention each of the columns, and use an "isNull" for the offending fields

2- Alter the column in the table to add a default constraint for ''. What this will do, is if you attempt to insert a 'null', it will replace it with the empty string

3- Add a 'before insert' trigger which will alter the data before the insert, to not contain a 'null' anymore

PS: Are you positive that the replication system has that column marked as "required"? I think if it is not required, it will insert 'null' if no data exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文