Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

fn_varbintohexsubstring in schema bound function? Expand / Collapse
Author
Message
Posted Friday, April 5, 2013 5:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 2:17 PM
Points: 1, Visits: 37
Hi,

I've read that you can't use a system object in a schema-bound function, so is there a workaround? I want the 40 character sha-1 hash of sku's longer than 40 characters to be returned as a varchar(40) but I am getting the following error:

Cannot schema bind function 'dbo.bla' because it references system object 'sys.fn_varbintohexstr'.

I've also tried accessing it as "master.dbo.fn_varbintohexstr" as well as the "fn_varbintohexsubstring". I am on sql server 2005 so I've read that the CONVERT method won't work (needs 2008).

As for the reasoning of this function, I am trying to make a schema bound function to add to an indexed view with a better primary key to join on for the largest tables. the sku column is currently varchar(255) and joins on this really long primary key seem to be eating up the execution time for large queries. The number of entries greater than 40 characters is less than 10% of the data, so I'm not too worried about collisions.

Any advice is appreciated!


ALTER FUNCTION [dbo].[bla] 
(
-- Add the parameters for the function here
@sku varchar(200)
)
RETURNS varchar(40)
WITH SCHEMABINDING
AS
BEGIN
-- Declare the return variable here
DECLARE @ResultVar varchar(40)

-- Add the T-SQL statements to compute the return value here
SELECT @ResultVar = (case when (LEN(@sku) <= 40) THEN @sku ELSE (sys.fn_varbintohexstr(HASHBYTES('SHA1',@sku))) END)

-- Return the result of the function
RETURN @ResultVar

END

Post #1439529
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse