Why can't I set this computed column to Persisted?

I have a computed column in SQL Server 2008 with the following formula definition:


Where IPAddress is another column in the table
When I try to set the Persisted flag to true, I am told that the column is not Deterministic - but that makes no sense to me given the function provided below.  I made sure not to use a Float or other such data types - what is wrong here?
FUNCTION dbo.fncDot2LongIP( 
	@IP VarChar(15) 
	DECLARE @ipA BigInt, @ipB Int, @ipC Int, @ipD Int;
	DECLARE @thisPlace Int;
	SET @thisPlace = CHARINDEX('.',@ip);
	SET @ipA = SUBSTRING(@ip,0,@thisPlace);
	SET @ip = SUBSTRING(@ip,@thisPlace + 1, LEN(@ip));
	SET @thisPlace = CHARINDEX('.',@ip);
	SET @ipB = SUBSTRING(@ip,0,@thisPlace);
	SET @ip = SUBSTRING(@ip,@thisPlace + 1, LEN(@ip));
	SET @thisPlace = CHARINDEX('.',@ip);
	SET @ipC = SUBSTRING(@ip,0,@thisPlace);
	SET @ipD = SUBSTRING(@ip,@thisPlace + 1, LEN(@ip));
   RETURN ( @ipA * 256*256*256 ) + ( @ipB * 256*256 ) + ( @ipC * 256 ) + @ipD

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You have to SELECT the value from the function:

(select dbo.fncDot2LongIP('') AS IPNum)
ktolaAuthor Commented:
Thak you for your quick response.  I tried entering this as the Formula:
 (Select dbo.fncDot2LongIP(IPAddress) as IPNum)
As I get the following error:
 Subqueries are not allowed in this context. Only scalar expressions are allowed.
mmm, just created a test table with an IP address varchar(15) and passed it into the function as a SELECT:

select dbo.fncDot2LongIP(tblTest.IPAddress) AS IPNum from tblTest

Worked perfectly. Can you post your SQL query so we can look at that?
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

ktolaAuthor Commented:
That would work without an issue - however I am using the statement as the formula in a computed column.  To test this out, simply create a table with 2 columns - IPAddress VarChar(15) and LongIP.  For LongIP, start with my base formula:

Once the table is created, then try to insert an IPValue - this will work fine but the LongIP is virtual and you cannot persist the data.
I need to know why the funciton is considered to be non-deterministic.
Tim HumphriesDirectorCommented:
To use a UDF as a persisted column, you must use WITH SCHEMABINDING in the function definition.

Your fucntion works fine in this context if you do that. Simply place WITH SCHEMABINDING after the RETURNS clause when creating the function.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ktolaAuthor Commented:
That was it - thanks!  I thought the WITH SCHEMABINDING was just for table functions and fields like XML.  I did not realize you could use it for scalar returns as well.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.