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
-- 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