Issa S.
asked on
SQL to UPDATE one field in another field in a Table which has only one Record
Hi All,,
I made this SQL to pick up one value from the top row of file.
How cab I convert this on value result of this SQL to UPDATE one field in another field in a Table which has only one Record.
The field in the second table to be updated , say AAA in table ABCD.
SELECT TOP 1 [TimeRange].BankBal
FROM [TimeRange];
The above select is picking the correct value that I need.
Optionally, Can I send the value of this SQL into a bound object (or even unbound) in an open form?
Thanks
I made this SQL to pick up one value from the top row of file.
How cab I convert this on value result of this SQL to UPDATE one field in another field in a Table which has only one Record.
The field in the second table to be updated , say AAA in table ABCD.
SELECT TOP 1 [TimeRange].BankBal
FROM [TimeRange];
The above select is picking the correct value that I need.
Optionally, Can I send the value of this SQL into a bound object (or even unbound) in an open form?
Thanks
You can update like below..
Update x
Set x.aaa = t.Bankbal
From abcd x inner join TimeRange t
On x.keyfield = t.keyfield
Update x
Set x.aaa = t.Bankbal
From abcd x inner join TimeRange t
On x.keyfield = t.keyfield
ASKER
Dear Pawan,
No Common fields to join on.
Dear Ste5an,
I will try and let you know soon.
Thanks
No Common fields to join on.
Dear Ste5an,
I will try and let you know soon.
Thanks
You can do like below
Update x
Set x.aaa = t.Bankbal
From abcd x inner join TimeRange t
On 1=1
Update x
Set x.aaa = t.Bankbal
From abcd x inner join TimeRange t
On 1=1
ASKER
Dear Ste5an,,
I did your suggestion but when Running, it gives an error saying: 'Operation must use an updateable query'.
I don't understand more of this as it doesn't much more information.
:(
I did your suggestion but when Running, it gives an error saying: 'Operation must use an updateable query'.
I don't understand more of this as it doesn't much more information.
:(
Pls try my last suggestion.
We can also do like below -
DataType you can choose based on what you have.
DataType you can choose based on what you have.
--
DECLARE @AAA AS DECIMAL(30,2)
SELECT TOP 1 @AAA = [TimeRange].BankBal FROM [TimeRange]
UPDATE ABCD
SET AAA = @AAA
--
This will work:
Here's the sql Fiddle: http://sqlfiddle.com/#!6/a9779/3/0
update a
set a.AAA = b.BankBal
from
abcd a
cross join (select top 1 bankbal from timerange) b;
select * from abcd
Here's the sql Fiddle: http://sqlfiddle.com/#!6/a9779/3/0
Just a note if you're running it in access you have to do the select first.
FROM:
https://www.techonthenet.com/access/queries/update2_2007.php
FROM:
https://www.techonthenet.com/access/queries/update2_2007.php
UPDATE abcd cross join (select top 1 bankbal from timerange) as TimeRange
set ABCD.AAA = TimeRange.BankBal
Use DLookup. Plain and easy:
Update ABCD
Set AAA = DLookup("[BankBal]","[TimeRange]");
/gustav
ASKER
Dear Gust,,
How to make sure that I am picking from the top record of the TimeRange ?
I tried it but it is picking from the last record!!!
Thanks
How to make sure that I am picking from the top record of the TimeRange ?
I tried it but it is picking from the last record!!!
Thanks
Neither did TOP 1 do that on its own.
But you can use:
But you can use:
Update ABCD
Set AAA = DLookup("[BankBal]","[TimeRange]","[DateField] = (Select Max(DateField) From TimeRange)");
/gustav
ASKER
Dear Gustav:
My real test Statement is:
Update [Copy of Sanad cushist Record]
Set retvalue = DLookup("[BankBal]"," [Bashitee=Statement Of Account-TimeRange] ");
What am after is to update the Retvalue in Copy of [Copy of Sanad cushist Record] with the [BankBal] in the First record in Table [Bashitee=Statement Of Account-TimeRange].
When I run it, it gives error 'Unkown' and points to [RetValue] !!!!?
Thanks again
My real test Statement is:
Update [Copy of Sanad cushist Record]
Set retvalue = DLookup("[BankBal]"," [Bashitee=Statement Of Account-TimeRange] ");
What am after is to update the Retvalue in Copy of [Copy of Sanad cushist Record] with the [BankBal] in the First record in Table [Bashitee=Statement Of Account-TimeRange].
When I run it, it gives error 'Unkown' and points to [RetValue] !!!!?
Thanks again
You can do like below - Below will pick the latest record.
Access-
UPDATE abcd cross join (select top 1 bankbal from timerange ORDER BY YourDateColumn DESC ) as TimeRange
set ABCD.AAA = TimeRange.BankBal
or
For SQL Server
DECLARE @AAA AS DECIMAL(30,2)
SELECT TOP 1 @AAA = [TimeRange].BankBal ORDER BY yourDateColumn DESC FROM [TimeRange]
UPDATE ABCD
SET AAA = @AAA
Access-
UPDATE abcd cross join (select top 1 bankbal from timerange ORDER BY YourDateColumn DESC ) as TimeRange
set ABCD.AAA = TimeRange.BankBal
or
For SQL Server
DECLARE @AAA AS DECIMAL(30,2)
SELECT TOP 1 @AAA = [TimeRange].BankBal ORDER BY yourDateColumn DESC FROM [TimeRange]
UPDATE ABCD
SET AAA = @AAA
A bit confusing with all those field and/or table names, but it might read:
Update [Copy of Sanad cushist Record]
Set Retvalue = DLookup("[BankBal]","[Statement Of Account-TimeRange]","[Bashitee] = (Select Max([Bashitee]) From [Statement Of Account-TimeRange])");
/gustav
ASKER
Dear Gust,,
The same error is still showing : 'Unkown' and points to [RetValue]'
The same error is still showing : 'Unkown' and points to [RetValue]'
That is the field name, so an error means that it either is non existing or misspelled.
/gustav
/gustav
ASKER
Dear Gust,,,
I checked and re-check. It is the correct name. To make sure it is the right field, I run it in the Datasheet view and it shows the correct value in the destination field that has to be replaced with the new value.
The same error appears when I RUN it.
I will send you screen shots in a minutes.
Thanks for your patience.
I checked and re-check. It is the correct name. To make sure it is the right field, I run it in the Datasheet view and it shows the correct value in the destination field that has to be replaced with the new value.
The same error appears when I RUN it.
I will send you screen shots in a minutes.
Thanks for your patience.
It is what you originally called "field AAA in table ABCD".
/gustav
/gustav
ASKER
Yes like this:
"The field in the second table to be updated , say AAA in table ABCD."
Also,
This is my bare select before converting it to the Update, which is still picking the correct value.
SELECT TOP 1 [TimeRange].BankBal
FROM [TimeRange];
"The field in the second table to be updated , say AAA in table ABCD."
Also,
This is my bare select before converting it to the Update, which is still picking the correct value.
SELECT TOP 1 [TimeRange].BankBal
FROM [TimeRange];
First, check that you get a value from DLookup. Press Ctrl+G and enter:
Next, switch the query to SQL view and double-check that the code looks similar to what I posted above:
https://www.experts-exchange.com/questions/29065359/SQL-to-UPDATE-one-field-in-another-field-in-a-Table-which-has-only-one-Record.html?anchorAnswerId=42348317#a42348317
/gustav
? DLookup("[BankBal]","[Statement Of Account-TimeRange]","[Bashitee] = (Select Max([Bashitee]) From [Statement Of Account-TimeRange])")
Correct if needed.Next, switch the query to SQL view and double-check that the code looks similar to what I posted above:
https://www.experts-exchange.com/questions/29065359/SQL-to-UPDATE-one-field-in-another-field-in-a-Table-which-has-only-one-Record.html?anchorAnswerId=42348317#a42348317
/gustav
Well, then:
and:
? DLookup("[BankBal]","[TimeRange]","[Bashitee] = (Select Max([Bashitee]) From [TimeRange])")
and:
Update [Copy of Sanad cushist Record]
Set Retvalue = DLookup("[BankBal]","[TimeRange]","[Bashitee] = (Select Max([Bashitee]) From [TimeRange])");
/gustav
ASKER
The Real Sorry the above is a simulated SQL.
The real one is :
SELECT TOP 1 [Bashitee=Statement Of Account-TimeRange]![BankBa l] AS RetValue
FROM [Bashitee=Statement Of Account-TimeRange];
The result of the Select Query is :
Which is also picking the correct value that should replace the 333 value in the above 1st screen shot.
The real one is :
SELECT TOP 1 [Bashitee=Statement Of Account-TimeRange]![BankBa
FROM [Bashitee=Statement Of Account-TimeRange];
The result of the Select Query is :
Which is also picking the correct value that should replace the 333 value in the above 1st screen shot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
am sorry for taking much of your time but it is really getting on my serves.
Then debug again as explained here:
https://www.experts-exchange.com/questions/29065359/SQL-to-UPDATE-one-field-in-another-field-in-a-Table-which-has-only-one-Record.html?anchorAnswerId=42348619#a42348619
And do double-check your weird table names.
/gustav
https://www.experts-exchange.com/questions/29065359/SQL-to-UPDATE-one-field-in-another-field-in-a-Table-which-has-only-one-Record.html?anchorAnswerId=42348619#a42348619
And do double-check your weird table names.
/gustav
ASKER
OK, sure
I will also prepare a small isolated DB and may pass it to you.
I will also prepare a small isolated DB and may pass it to you.
ASKER
Dear Gustav,,,
Wonderfulllllllllll Now working.
I am so sorry for the above hassle due to being under pressure.
My SQL now is:
UPDATE [Sanad Record] SET [Sanad Record].Retvalue = DLookUp("[BankBal]","[Time Range]","[ Date] = (Select Min([date]) From [TimeRange])");
as in my simulated DB that I was about to send to. I only changed the MAX to MIN since my dates are in ascending order.
Tons of thanks
Wonderfulllllllllll Now working.
I am so sorry for the above hassle due to being under pressure.
My SQL now is:
UPDATE [Sanad Record] SET [Sanad Record].Retvalue = DLookUp("[BankBal]","[Time
as in my simulated DB that I was about to send to. I only changed the MAX to MIN since my dates are in ascending order.
Tons of thanks
Great! Then please mark as answered.
/gustav
/gustav
Closing - Accepting verified solution by Author
Open in new window
But without ORDER BY you'll get an arbitrary value from TimeRange.