Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Evaluation time for functions SQL2005 / SQL2000 Expand / Collapse
Posted Monday, March 16, 2009 12:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 19, 2016 5:02 AM
Points: 2, Visits: 95
I have a database that has recently migrated from SQL 2000 to SQL2005. I have a problem with the evaluation of a function which did not cause problems in SQL 2000 but does in 2005.

The function ( getNextID() ) generates a new ID value for a table. There is no Identity column defined on that table, its just an int column and this function is used to generate the next sequential value (... I inherited this database, I don't know why they didn't just define an Identity column).

The problem arises when inserting more than one new row with code like this:
INSERT INTO Project (ProjectID, [Description], [Manager])
SELECT dbo.GetNextID(), [ProjectDescription], [ProjectManager]
FROM tmpImportTable_Project

In SQL 2000 this works correctly when you are inserting > 1 new row. That is, the insert statement correctly generates a sequential value for each new row.
In SQl 2005 however, the function seems to be evaluated for all new rows prior to any rows being inserted, and not evaluated after each and every new row is inserted. So it doesn't generate a sequential value for each new row, it generates the same value for each and every row. Any inserts then fail on the second row because of a PK violation.

I can write a bit of script to generate the required sequential IDs; thats not a problem. But I wondered if anyone had met this problem before and if they had a resolution for it?
Is there perhaps a global setting in 2005 that would affect the evaluation-time of functions?

Kind regards,
Post #676815
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse