This question already has answers here:
How to update two tables in one statement in SQL Server 2005?
(10 answers)
Closed 2 years ago.
I have a problem. I have a 2 table which is table orders and table delivery. table order has column order_recieved. by default NULL. and table delivery has column delivery_status with value order shipped. now, if customer click a button to update order_received, the column will change NULL value to Recieved value. and my problem is how column delivery_status will also change to order shipped value to complete value? the PK table orders is order_no and FK table delivery is orderFK.
Can someone help me how to update the value?
You can only update one table per update statement. I'd recommend sticking them both in the same stored procedure so that you can make one database call and have both values updated.
Related
This question already has an answer here:
How to divide all columns of a numeric table by the last column in SQL Server? [closed]
(1 answer)
Closed 2 years ago.
In SQL Server I am going to divide all columns of a table by the last column in that ID column and the last column itself are not included.
Suppose the data is numeric.
The simple way is to use the following code consider with 4 columns and one ID column:
update Table1
set col1 = col1/col4, col2 = col2/col4, col2 = col2/col4
where id = ...
and this must be repeated for updating other records too.
The problem is that we have no idea how many columns are there. So, it seems we need some variables and and loops to detect columns and update them for each record.
and after updating
I need a code to do it for any table with numeric values.
This question already has answers here:
How do I alter the position of a column in a PostgreSQL database table?
(10 answers)
Change column order in table of postgres
(1 answer)
How to change the position of column in postgresql without dumping
(1 answer)
Closed 2 years ago.
When a new column is added to a current table, this appears in the last column. eg:
added AGE columns to Students table
SELECT * FROM Students; "would show the AGE column right at the end.
Question: is it possible to change the order of these columns when such commands are executed?
If you have an existing table, you could create a VIEW with the columns reorganized, then of course, you could also type out the columns. You could also create a new table.
This question already has answers here:
How to get the identity of an inserted row?
(15 answers)
Closed 5 years ago.
Here's what I'm trying to do: I add a record to the table. This record receives its unique ID number (identity). Then I want to use this ID in my code. How can I find out which ID number has just received a newly added record in my SQL Server database table?
Add an output parameter to your insert statement and return scope_identity(). If this isn't sufficient just search my answer, it will be in here a bunch.
This question already has answers here:
Upserting in MS-access
(6 answers)
Closed 6 years ago.
I know this question must have been asked a million times but I have yet to find a "NON-VBA" solution to this in MS Access 2007 SQL.
I am using 2 Tables TBLDestination and TBLSource. I have to update the destination table records from source table records when a matching ID is found. For a non-matching ID (i.e. new ID) I want to insert the new record
(Please refer the tables below).
-----TBLSource-------
ID (match if ID exists in Destination table)
EmpName
EmpAdd
---TBLDestination-----
ID
EmpName (to be updated/inserted)
EmpAdd (to be updated/inserted)
Salary
Access DOES have the equivalent using an UPDATE with a LEFT JOIN. See here.
MS Access doesn't have an equivalent to UPSERT where you can do both in a single query.
However, you can get the same effect by doing an UPDATE query where you join to the table you want to update from. Then you do an INSERT query where you OUTER JOIN to the table and return only those where it doesn't exist.
This question already has answers here:
Is there a "Default Order By Column" in SQL Server?
(2 answers)
How to SELECT the last 10 rows of an SQL table which has no ID field?
(13 answers)
Closed 9 years ago.
I need to write a SQL query that returns records in the order in which they were entered in to a table.
I can't add a column to the table or change the table in any way. I can insert into and select from the table.
Say I execute three insert queries like
insert into x values(a,1);
insert into x values(d,4);
insert into x values(u,42);
when I select from the table x I need to get the records in this order.
a 1
d 4
u 42
The table has only two columns, both have nothing to do with date.
You can't do it without changing the table in some way.
When you select data from a table the order in which it is returned is non-deterministic on all the sql database engines I know and certainly in MSSQL server 2000+. To get the rows in a defined order you must include an ORDER BY clause and there is nothing you can specify to give the desired order.
Since you cannot change the schema, then this is game over.
Okay, (almost) nothing is impossible. You could periodically analyse the physical database file for changes and decode those into the information you require but, this would likely fail when multiple rows were inserted by one transaction.
I doubt you have the access or the inclination to do that.
You can't. The order returned is not deterministic without an order by clause, and you don't have any thing to order by