I have a table named products
.
Products
Quantity
5
I need to update Quantity
by any value; for example, adding 3 to the current value, giving output like below:
Quantity
8
How can I write an SQL query to accomplish this?
INSERT INTO Syntax Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)
To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.
1. ALTER Command : ALTER is an SQL command used in Relational DBMS and is a Data Definition Language (DDL) statement. ALTER can be used to update the table's structure in the database (like add, delete, drop indexes, columns, and constraints, modify the attributes of the tables in the database).
update products
set quantity = quantity + 3
declare @table table(id int, quantity int)
insert into @table values(1, 5)
update @table
set quantity = quantity + 3
output inserted.quantity
Assuming that you actually wanted the value outputted. Don't forget any where clauses that may be needed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With