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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Updating a field
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

andypgill
Starting Member

34 Posts

Posted - 02/06/2013 :  12:22:11  Show Profile  Reply with Quote
Hi

Can anyone help me with my code.

I have a table called [Draft Version] with a field called draft. This field contains one number.

My main table is called budgets and it contains a field called draft.

I basically want to updated the budgets.draft with the number that is in [Draft Version] if the budgets.draft is null.

This is my code

UPDATE Budgets
SET draft = (SELECT [Draft] FROM [Draft Version]
where Budget.draft is null)

The problem I'm getting is that it is updating the field irrespective of whether it is null or not.

Thanks

James K
Flowing Fount of Yak Knowledge

3661 Posts

Posted - 02/06/2013 :  12:45:20  Show Profile  Reply with Quote
Is there just one row in each table? Assuming that there are many rows in the Budget table that are NULL and there are many rows in [Draft Version] where draft is something other than NULL, which values from the [Draft Version] should go into which rows in Budget table? In other words, is there another column (or may be more than one column) that can be used to relate a given row in the Budget table to a row in [Draft Version] table?
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
333 Posts

Posted - 02/06/2013 :  14:25:56  Show Profile  Reply with Quote
If there is no related fields you could try:

UPDATE B
SET Draft = (SELECT TOP(1) [Draft] FROM [Draft Version])
FROM Budgets
WHERE Draft IS NULL


djj
Go to Top of Page

andygill
Starting Member

3 Posts

Posted - 02/07/2013 :  02:13:50  Show Profile  Reply with Quote
quote:
Originally posted by James K

Is there just one row in each table? Assuming that there are many rows in the Budget table that are NULL and there are many rows in [Draft Version] where draft is something other than NULL, which values from the [Draft Version] should go into which rows in Budget table? In other words, is there another column (or may be more than one column) that can be used to relate a given row in the Budget table to a row in [Draft Version] table?



There is just one row and one field in [draft version] there will be many rows in budget which will have a null draft field.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3661 Posts

Posted - 02/07/2013 :  04:59:42  Show Profile  Reply with Quote
Since there is only one row in [Draft Version], djj's code should work.
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.05 seconds. Powered By: Snitz Forums 2000