how to update the multiple rows at a time in aspnetboilerplate? - sql

I want to execute the SQL clause like:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Because I want to update multiple rows in database. But I don't know how to implement this.

I have found the solution. Using the extension method GetDbContext of Repository to get the DbContext, then using the DbContext.Database.ExecuteSqlCommandAsync to execute raw SQL.

Related

sql: how to select all records with field set to 0 and set the value of that field to one

Is this possible in one SQL statement?
I pointer to a nice SQL tutorial on the subject is also appreciated.
I know I can use a command like SELECT * FROM MYTAB WHERE MYFIELD = 0
and then use a server script to go through the result and UPDATE MYFIELD
of the result rows.
Wouldn't this work?
UPDATE MYTAB SET MYFIELD=1 WHERE MYFIELD=0
Edited to Add:
If you are interested in SQL tutorials, a lightweight one that has the nice feature of allowing you edit and run SQL commands in the webpage to see how they work is from w3schools here: http://www.w3schools.com/sql/
If you can execute a query to read the data you should also be able to write the data;
UPDATE MYTAB SET MYFIELD = 99 WHERE MYFIELD = 0
UPDATE (Transact-SQL)
UPDATE Statement (Oracle)
UPDATE Syntax (MySQL)
UPDATE Statement (PostgreSQL)
UPDATE query..
UPDATE TABLE
SET FIELD = 1
WHERE FIELD = 0
I think it should be as simple as:
UPDATE MYTAB
SET MYFIELD = 1
WHERE MYFIELD = 0
Hope this helps!
Why do you want to execute a SELECT query when your purpose can be solved by a simple UPDATE query? It seems that you want to switch the flag column of your table.
how to select all records with field set to 0 and set the value of
that field to one
Use below query.
Update MYTAB
set MYFIELD=1
Where MYFIELD=0
This query will find all records where MYFIELD is set to 0 and will update all those records with value 1.
You can browse thru following links to learn advance SQL.
SQLCourse2.com
SQL Tutorial
Tutorials Point

Update multiple values in an oracle table using values from an APEX collection

I am using APEX collections to store some values and pass them between pages in Oracle Application Express 4.2.3.
I would like to then perform an update statement on a table called "project" with the values from the collection.
My code so far is as follows:
update project
SET name=c.c002,
description=c.c007,
start_date=c.c004,
timeframe=c.c005,
status=c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
and id = :p14_id;
where :p14_id is the value of a page item.
However, I am getting the following error:
ORA-00933: SQL command not properly ended
Anyone have any idea on how to approach this?
Thanks!
The UPDATE syntax you are using is not valid in Oracle; it does not allow you to use FROM in the way you are attempting.
The simplest way to do this in Oracle would with a subquery:
update project
set (name, description, start_date, timeframe, status) =
(select c.c002, c.c007, c.c004, c.c005, c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
)
WHERE
id = :p14_id
;
Note that if the subquery returns no rows, the columns in the target table will be updated to NULL; this could be avoided by adding a similar EXISTS condition in the predicate for the update. It could also be avoided by using a MERGE statement instead of an UPDATE.
If the subquery returns multiple rows, the statement will throw an error. It looks like tthat should not be the case here.

SQL Match and Replace

I have to update a table in my sql server with data from an excel sheet It is a lot of parts I need to update and I need to update two columns in a 12 column table. What would be the easiest way doing this?
I was thinking access? I tried to do a SSIS job on the specific two columns but it failed.
Please help and thanks.
One way of doing it is to import the Excel file into your sql database and then do the update with a simple update statement like his:
UPDATE YourTable
SET Column1 = ExcelImported.Column1,
Column2 = ExcelImported.Column2
FROM YourTable
JOIN ExcelImported
ON YourTable.Key = ExcelImported.Key

Execute raw SQL using ServiceStack.OrmLite

I am working ServiceStack.OrmLite using MS SQL Server. I would like to execute raw SQL against database but original documentation contains description of how to do it with SELECT statement only. That is not enough for me.
I cant find the way to run anything as simple as that:
UPDATE table1
SET column1 = 'value1'
WHERE column2 = value2
Using, for example:
var two = db.Update(#"UPDATE table1
SET column1 = 'value1'
WHERE column2 = value2");
Running this expressions with db.Update() or db.Update<> produces uncomprehensive errors like
Incorrect syntax near the keyword 'UPDATE'.
I would like to use raw sql because my real UPDATE expression uses JOIN.
db.Update is for updating a model or partial model as shown in OrmLite's Documentation on Update. You can choose to use the loose-typed API to build your update statement, e.g:
db.Update(table: "table1",
set: "column1 = {0}".Params("value1"),
where: "column2 = {0}".Params("value2"));
The Params extension method escapes your values for you.
Otherwise the way to execute any arbitrary raw sql is to use db.ExecuteSql().
If it is a SELECT statement and you want to execute using raw sql you can use:
List<Person> results = db.SqlList<Person>("SELECT * FROM Person WHERE Age < #age", new { age=50});
Reference: https://github.com/ServiceStack/ServiceStack.OrmLite#typed-sqlexpressions-with-custom-sql-apis

How to write a update SQL using a select SQL

I want to write a update SQL statement, but one conidtion of this statement is the result from a select SQL statement, and I also want to return the result of the select SQL statement.
Like this: update ... set ... where id = (select id from ...)
I want to return the value of id back.
Does anybody know how should I do this?
Thanks in advance!
I don't believe that's possible in one statement. Update then query (select) the new value, or query the value first, and then submit an update.
Alternative would be a stored procedure on the database, which executes the multiple queries and returns the result for you.
This is not possible in all Java database frameworks that I know. Probably you need to separate your query and update in Java.
I don't see any problem in using a subselect in a WHERE clause of an update statement.
For the second request, getting back the value of id, I know this is possible in DB2, and maybe others implement that syntax too:
SELECT id FROM FINAL TABLE (
update ... set ... where id = (select id from ...)
)
This works also for INSERT and DELETE statements. (See the documentation.)
Update statements won't return the updated datasets. The select in that case would be a subselect that isn't directly accessible.
You'd thus have to use at least two queries:
select the ids you want
call the update query passing the previously selected ids as a parameter