SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Null values to = unique ID to allow No duplicates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  09:59:55  Show Profile  Reply with Quote

Ok I have upgraded my works database from a poorly designed Access database to a SQL database. The previous system allowed NULL values and duplicates to be inserted into a field that should NOT ALLOW NULL Values or duplicates. Therefore, this issue has now been moved across to my new system as I cannot set these constraints on the field that has multiple NULL values.

My solution would be to use a sequential operator, so whatever = NULL would be changed to a sequential number that us as administrators would know was a bogus number starting at something like = 999999900 counting up from that. There are only 250 records that would require updating.

To make things more interesting this field is not a integer type, its a Varchar type as its a Hardware ID. Both numerical and characters are require.

I cannot get my code to work

UPDATE tblAsset SET HardwareNumber = r.NextID FROM tblAsset cross join (select 10000000 - ROW NUMBER() over (order by RAND()) AS NextID FROM tblAsset WHERE HardwareNumber is NULL) r WHERE tbl.Asset.HardwareNumber is NULL - This code only changes all NULL values in the HardwareID field to 99999999.

Please HELP.

visakh16
Very Important crosS Applying yaK Herder

India
47024 Posts

Posted - 06/17/2012 :  10:55:14  Show Profile  Reply with Quote
so you want NULL values to be


Posted - 06/17/2012 : 09:59:55

Ok I have upgraded my works database from a poorly designed Access database to a SQL database. The previous system allowed NULL values and duplicates to be inserted into a field that should NOT ALLOW NULL Values or duplicates. Therefore, this issue has now been moved across to my new system as I cannot set these constraints on the field that has multiple NULL values.

My solution would be to use a sequential operator, so whatever = NULL would be changed to a sequential number that us as administrators would know was a bogus number starting at something like = 999999900 counting up from that. There are only 250 records that would require updating.

To make things more interesting this field is not a integer type, its a Varchar type as its a Hardware ID. Both numerical and characters are require.

I cannot get my code to work

UPDATE r
SET HardwareNumber = r.NextID FROM (select 10000000 - ROW NUMBER() over (order by HardwareNumber) AS NextID FROM tblAsset WHERE HardwareNumber is NULL) r 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  11:23:49  Show Profile  Reply with Quote
I want the NULL values to be a unique number with an increment of 1. Starting at 999999900
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47024 Posts

Posted - 06/17/2012 :  11:31:26  Show Profile  Reply with Quote
thsn shouldnt it be?

UPDATE r
SET HardwareNumber = r.NextID FROM (select  999999899 + ROW NUMBER() over (order by HardwareNumber) AS NextID FROM tblAsset WHERE HardwareNumber is NULL) r 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  11:51:12  Show Profile  Reply with Quote
What do you suggest? Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47024 Posts

Posted - 06/17/2012 :  11:51:43  Show Profile  Reply with Quote
first try and see if that gives you intended result...

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  14:24:51  Show Profile  Reply with Quote
No luck. I have tried the following but I am getting the error Incorrect syntax near the keyword OF? Any suggestions.

DECLARE @@counter int
SET @@counter = 999999900
DECLARE UpdateRecords CURSOR FOR SELECT HardwareNumber FROM tblAsset WHERE HardwareNumber IS NULL

OPEN UpdateRecords
FETCH NEXT FROM UpdateRecords;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @@counter = @@counter + 1
UPDATE tblAsset SET HardwareNumber = @@counter
WHERE CURRENT OF UpdateRecords;
FETCH NEXT FROM UpdateRecords;
END
CLOSE UpdateRecords
DEALLOCATE UpdateRecords
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47024 Posts

Posted - 06/17/2012 :  15:02:52  Show Profile  Reply with Quote
this is not even near to what i suggested.
Why are you using cursor? you just needed the suggestion i gave. it does id assignment in a batch

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  15:38:29  Show Profile  Reply with Quote
Hi, very sorry only just seen the amended version. I tried it and have got error Msg 102, Level 15, State 1, Line 6. Incorrect syntax near r?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47024 Posts

Posted - 06/17/2012 :  15:49:58  Show Profile  Reply with Quote
are you sure you were using below suggestion itself?

UPDATE r
SET HardwareNumber = r.NextID 
FROM (select  999999899 + ROW NUMBER() over (order by HardwareNumber) AS NextID,HardwareNumber FROM tblAsset WHERE HardwareNumber is NULL) r 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  16:00:22  Show Profile  Reply with Quote
Im now getting msg 102, level 15, state 1, line 3 incorrect syntax near '('
Go to Top of Page

rustynails28
Starting Member

United Kingdom
12 Posts

Posted - 06/17/2012 :  16:03:26  Show Profile  Reply with Quote
Well done, I just changed a parameter and it works. Well done thanks for your help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47024 Posts

Posted - 06/17/2012 :  16:12:06  Show Profile  Reply with Quote
welcome

in future please try to use suggestion as given to avoid confusion!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000