mssql update seems to be happening too quickly for function

Posted on 2009-02-23
Last Modified: 2012-05-06
Greetings.  I have a function I have written to generate a unique key / purl for each record in one of my tables.  below in the "code" is my function.

the function works well, except when i run:
SET              PURL = dbo.funcGenerateUniquePURLByName(firstname, lastname)
where PURL is null

things seem to be happening too quickly.  i end up with firstnamelastname for all of my records instead of firstnamelastname, firstnamelastname1, etc.

any help would be appreciated.

alter FUNCTION funcGenerateUniquePURLByName


	-- Add the parameters for the function here

	@FirstName as varchar(200),

	@LastName as varchar(200)


RETURNS varchar(200)



	-- Declare the return variable here

	DECLARE @Count as int

	Set @Count = 0

	DECLARE @NewPURL as varchar(200)

	SET @FirstName = CAST(REPLACE(@FirstName,' ','') as varchar(100))

	SET @LastName = CAST(REPLACE(@LastName, ' ', '') as varchar(100))

	--SET @NewPURL = (select 'JohnHENDERSON' as expr1)

	SET @NewPURL = @FirstName+@LastName

	-- Add the T-SQL statements to compute the return value here

while ((select count(PURL) from ICM_GUAMaster With (tablockx, holdlock) where PURL = @NewPURL) = 1)


	set @Count=@Count+1

	set @NewPURL = (REPLACE(@FirstName,' ','')+REPLACE(@LastName, ' ', '')+cast(@Count as varchar(20)))


	-- Return the result of the function




Open in new window

Question by:rugby148
    1 Comment
    LVL 16

    Accepted Solution

    No its not that it is happening to fast (it would never do that, it always waits for the function to return the value first) but since you are doing a bulk update it is finding the value to insert based off the data at the time of the query (as if you were doing a select).

    You need to do your query as a loop or something and do one record at a time.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now