Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


fn_varbintohexsubstring in schema bound function?


fn_varbintohexsubstring in schema bound function?

Author
Message
jshanman
jshanman
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search