开发者

Use master.sys.fn_varbintohexsubstring in computed column

开发者 https://www.devze.com 2023-03-24 04:07 出处:网络
In my sql server table, I want to add a computed column that is a hash of other columns in the same table. Below is my table structure.

In my sql server table, I want to add a computed column that is a hash of other columns in the same table. Below is my table structure.

Address:

AddressID(int , PK)

AddressLine1 (nvarchar)

AddressLine2 (nvarchar)

City (nvarchar)

State (nvarchar)

AddressHash(computed column)

Below is what I want to get in my computed column:

MASTER.SYS.FN_VARBINTOHEXSUBSTRING(0, HASHBYTES('SHA1',COALESCE(AddressLine1, N'') + COALESCE(AddressLine2, N'') + COALESCE(City, N'') + COALESCE(State, N'')), 1, 0)

If I right-click the table and go to design and enter the above for "Formula" under "Computed Column Specification", I get the following error:

- Unable to modify table.

A user-defined function name cannot be prefixed with a database name in this context.

So I thought I would use a user defined function to calculate the hash and map that udf to formula.

Below is the code that I am using to create UDF:

CREATE FUNCTION udfHashAddress
(   
    @pAddressLine1 nvarchar(50), @pAddressLine2 nvarchar(50), @pCity nvarchar(50), @pState nvarchar(50))
)
RETURNS nvarchar(max) -- not sure what the correct size would be
WITH SCHEMABINDING
AS
BEGIN   
    DECLARE @result nvarchar(max)   
    SELECT @result = MASTER.SYS.FN_VARBINTOHEXSUBSTRING(0, HASHBYTES('SHA1',COALESCE(@pAddressLine1, N''开发者_StackOverflow) + COALESCE(@pAddressLine2, N'') + COALESCE(@pCity, N'') + COALESCE(@pState, N'')), 1, 0)  
    RETURN @result
END
GO

But I get the following error with the above code:

*Cannot schema bind function 'udfHashAddress' because name 'MASTER.SYS.FN_VARBINTOHEXSUBSTRING' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.*

When I removed the "MASTER" db prefix I got this error:

*Cannot schema bind function 'udfHashAddress' because it references system object 'SYS.FN_VARBINTOHEXSUBSTRING'.*

Am I missing something here? Would appreciate any assistance/pointers.


Since you're using SQL Server 2008, have you tried simply:

CONVERT(VARCHAR(MAX), HASHBYTES('SHA1','string'), 2);

This will return upper case instead of lower case letters, but you can fix that with LOWER() if it's important.

Here is an example with your actual schema (created in tempdb on purpose):

USE tempdb;
GO

CREATE TABLE dbo.[Address]
(
    AddressID INT PRIMARY KEY,
    AddressLine1 NVARCHAR(64),
    AddressLine2 NVARCHAR(64),
    City NVARCHAR(64),
    [State] NVARCHAR(64),
    AddressHash AS LOWER(CONVERT(VARCHAR(4000), HASHBYTES('SHA1', 
        COALESCE(AddressLine1, N'') + COALESCE(AddressLine2, N'') 
        + COALESCE(City, N'') + COALESCE([State], N'')), 2))
        --PERSISTED -- you could also persist it if desired
);

INSERT dbo.[Address]
    VALUES(1, 'foo', 'bar', 'blat', 'splunge'),
          (2, 'bar', 'foo', 'blag', 'splmger');

SELECT *, master.dbo.fn_varbintohexsubstring
    (0,
        HASHBYTES
        (
            'SHA1', 
            COALESCE(AddressLine1, N'') + COALESCE(AddressLine2, N'') 
            + COALESCE(City, N'') + COALESCE([State], N'')
        ), 1, 0)
        FROM dbo.[Address];
GO

DROP TABLE dbo.[Address];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号