Bulk insert using LINQ - linq

Let's say we want to insert some data from one table to another in SQL. We would do something like this:
INSERT INTO tableY
SELECT name, type, <other fields>
FROM tableX;
Here we can conveniently omit the ID from tableX being inserted to tableY, if it's an auto-generated column.
How would you do that in LINQ? I know you can get a list of items from tableX and iterate and do that. But can we do that using a single LINQ Query, without iterating?

It seems that Linq to Entities does not provide a way to that. You can check in here:
https://stackoverflow.com/a/9221551/1384539
On the same thread, there's a suggestion to do that, I hope it could help you:
https://stackoverflow.com/a/11974858/1384539

Related

PL/SQL: Looping through a list string

Please forgive me if I open a new thread about looping in PL/SQL but after reading dozens of existing ones I'm still not able to perform what I'd like to.
I need to run a complex query on a view of a table and the only way to shorten running time is to filter through a where clause based on a variable to which such table is indexed (otherwise the system ends up doing a full scan of the table which runs endlessly)
The variable the table is indexed on is store_id (string)
I can retrieve all the store_id I want to query from a separate table:
e.g select distinct store_id from store_anagraphy
Then I'd like to make a loop that iterate queries with the store_id identified above
e.g select *complex query from view_of_sales where store_id = 'xxxxxx'
and append (union) all the result returned by each of this queries
Thank you very much in advance.
Gianluca
In theory, you could write a pipelined table function that ran multiple queries in a loop and made a series of pipe row calls to return the results. That would be pretty unusual but it could be done.
It would be far, far more common, however, to simply combine the two queries and run a single query that returns all the rows you want
select something
from your_view
where store_id in (select distinct store_id
from store_anagraphy)
If you are saying that you have tried this query and Oracle is choosing to do a table scan rather than using the index then what you really have is a tuning problem. Most likely, statistics on one or more objects are inaccurate which leads Oracle to expect that this query would return more rows than it really will thus favoring the table scan. You should be able to fix that by fixing the statistics on the objects. In a pinch, you could also use hints to force an index to be used.

How to rebuild data combination Power Query

In Power Query, I want to use a list of distinct values from one query (e.g. list of customers present on "Sales" table), to inject it on a SQL statement on another query (e.g. "Customer" dimensional table).
To pull the list of distinct values I have a function, getDistinct() that:
Retrieves one column from a query choice,
Only keep distinct values present on that column, and
Return these distinct values separated by commas so they can be injected within an SQL statement.
This function works fine on a standalone query. However, when I try to use it on my "Customer" query it throws an error (see code and error below):
let
Source = Oracle.Database("myServer", [Query="select * from db_customer where customer_id in (" & getDistinct(Sales,"CustomerID") & ")"])
in
Source
And the error:
Formula.Firewall: Query 'Customer' (step 'Source') references
other queries or steps, so it may not directly access a data source.
Please rebuild this data combination.
I've tried creating a different query that executes the function and then referencing it on my "Customer" query, but this doesn't seem to work. I know I can "Ignore Privacy Levels" (which by the way, I've checked and works), but since I don't know the implications of it, I'm afraid of leaked data.
I don't see why a function or any hand-written code is necessary for this requirement.
I would create a Query to get the Sales table and then Group by CustomerID. I would set that to: Load To / Only Create Connection.
Then the Customers Query would just be:
Source is Oracle Customers table
Merge to Sales Query on CustomerID, with Join Kind = Inner

Oracle update table value returned 'single-row sub-query returned more than one row '

I wanna add a column named 'Bonus_AMT' on table 'Employee'.
Here's the clause I wrote.
enter image description here
I ran the above clause, but it didn't work. It returns that 'single-row' sub-query return more than one row. How could I solve that?
You need to join table in update clause with table in select clause used in SET. Here is the example.
update employees e1 set bonus = (select salary*commission_pct from employees e2 where e1.employee_id = e2.employee_id);
You need to make sure that both versions of tables are joined on primary key.
Your select query clause used in set is returning multiple row for the applied date range. So you need to change that select clause so that at a time it will single record and update the same record using joins.

Oracle Trigger UPDATE instead of INSERT

Trying to find a way to write an Oracle trigger that would check before an insert to see if a match was found in the primary column and if so update the row information instead of inserting a new row.
I've looked at before insert. Is there a way to cancel the insert based on criteria inside that block?
I've also looked at using the instead of clause but it requires working on a view.
What is the best way to go about this?
Use a MERGE statement instead of an INSERT.
Use a merge statement.
MERGE INTO <<your table>> t
USING (<<your list of records - can be the result of a SELECT >>)
ON ( <<join between table and list of records >>)
WHEN MATCHED THEN
UPDATE SET << the rows you want to set>>
WHEN NOT MATCHED THEN
INSERT (<<columns of table>>)
VALUES (<<value>>)
https://oracle-base.com/articles/9i/merge-statement

select distinct from dataset

I have got duplicate rows in a dataset. how can i select distinct rows from that.
From comments: My query is something like this:
select name, age
from student
When I receive its output in a dataset the output consists of rows having duplicate names. Using dataset itself I have to select distinct name from this because I need the same query with duplicate values for some other place.
select DISTINCT name, age from student
If you need both the distinct data as well as the full data (with duplicate values), then you'll either have to maintain two datasets or continue doing things as you are now.

Resources