• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 76
  • Last Modified:

Query statement to update a secondary page

I have a building table with a unique ID.  I would like to run a query and update the variables in the secondary table.
select "NOR", "SOU","EAS","WES" FROM 'BLD1" WHERE 'SEQ' = "NY100350012"
UPDATE SURVEY_SPACE SET (NOR = $NOR.................

Can't this be done in one statement?
0
breeze351
Asked:
breeze351
  • 3
  • 3
  • 2
  • +1
1 Solution
 
arnoldCommented:
Which db tech is in use, question deals whether during the select you can as you illustrate assign one of the columns to a variable.

Select $NOR:="NOR".....
Oracle?

The other question is what is the application that does this update, seems.
The application should already have this information such that your update will include all requisite information.
0
 
breeze351Author Commented:
In the table "BLD1" the name is "SEQ".
In the table "BLD1" the name is "NOR".

In the table "Survey_Data" the name is "SEQ"
In the table "Survey_Data" the name is "NOR"

When I update "BLD1", I want to update "NOR" in all the records in "Survey_Data" with the same "SEQ" number.
0
 
arnoldCommented:
Not clear what situation, usually, the record insertion...
Understanding your process..

Table stracture/definition.for both

On what basis, what is the where clause on whose basis the record being updated can be distinguished from all other records?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Kyle AbrahamsSenior .Net DeveloperCommented:
in MS SQL Server you can do this with a join:

update sd
set sd.NOR = b.NOR
from
survey_data sd
join bld1 b on sd.SEQ = b.SEQ
where sd.Seq = @seq

Open in new window


Note that you can also do this from an update trigger so it happens automatically.

create trigger tr_BLD1_NOR
on dbo.bld1
after update
as
begin
    if update(NOR)
    begin
             update sd
             set sd.NOR = i.NOR
             from
             survey_data sd 
             join inserted i on sd.seq = i.seq
     end
end

Open in new window

0
 
breeze351Author Commented:
Wouldn't this work?
if ($up_down == "U")
	{
		$dst = "MT";
		$north = $_POST['Up_North'];	
		$south = $_POST['Up_South'];	
		$east = $_POST['Up_East'];	
		$west = $_POST['Up_West'];
	}
	else
	{
		$dst = "DT";
		$north = $_POST['Down_North'];	
		$south = $_POST['Down_South'];	
		$east = $_POST['Down_East'];	
		$west = $_POST['Down_West'];
        
$SqlString = "UPDATE survey_data
	SET
		NOR = '$north',
		SOU = '$south',
		EAS = '$east',
		WES = '$west'
	WHERE
	    SEQ = '$Building_Id'";
            
$conn->query($SqlString);            

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
You're calling it from the front end and it would require an extra trip to the database (I'm thinking that's PHP?)

If the logic is that it should be both values all the time it's better to do that in the database.

EG:  I run an update statement manually against the DB and break your logic.
0
 
breeze351Author Commented:
I've tried the following code:
$String = "UPDATE survey_data
SET
    survey_data.NOR = bld1.NOR,
    survey_data.SOU = bld1.SOU,
    survey_data.EAS = bld1.EAS,
    survey_data.WES = bld1.WES	
FROM
    INNER JOIN ON survey_data = bld1
WHERE
    survey_data.SEQ = bld1.SEQ";

$conn->query($String);

Open in new window

When I try it in PHPMyadmid. it doesn't like the join statement.

The error is :
An expression was expected. (near "INNER JOIN" at position 139)
0
 
slightwv (䄆 Netminder) Commented:
I'm not a SQL Server person but syntax from the database I am familiar with, you need to specify the table name in the FROM.

You also need to specify the ON clause of the join syntax instaad of the WHERE.

A quick Google seems to support that.

Try using the syntax here:
https://blog.sqlauthority.com/2013/04/30/sql-server-update-from-select-statement-using-join-in-update-statement-multiple-tables-in-update-statement/
0
 
arnoldCommented:
Update does not use from, it uses
Update tablename inner join table2 on tablename.survey_data=table2.bidl set tablename.column=

Your quert seems to equalize (copy existing data) from one table into another.

Usually, you need to compare columns not tables.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now