currently the system is creating multiples OledbConnections and the process is taking a long time to finish, it sums up to this:
C# code:
Select all person from table A.
FOR EACH person: Do a SELECT to see if ID 'x' from table A exists in table B.
FOR EACH person: INSERT with the ID from table A or a new ID (if that ID already exists in table B)
All these INSERTS are creating a new OledbConnection for each person, and for example 3k persons, it is taking too long.
It would be easier if I didn't have to deal with the IDs, but I'm not finding a good way to do this with VFP.
is that a way to Bulk INSERT this, or improve the performance?
if id in table2 is autonumber:
if exists(select * from table2 where id = idtable1)
insert into table2(field1,field2...
else
insert into table2(ID,field1,field2...
I don't understand why you need to use multiple OleDbConnections for this. A single connection would do:
string sql = #"insert into tableB (id, f1, f2, f3)
select getNextId('tableB'), f1, f2, f3
from tableA
where exists (select * from tableA where tableA.id = tableB.id)";
using (OleDbConnection cn = new OleDbConnection(#"Provider=VFPOLEDB;Data Source=c:\MyDataFolder"))
using (OleDbCommand cmd = new OleDbCommand(sql, cn))
{
cn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = #"insert into tableB (id, f1, f2, f3)
select id, f1, f2, f3
from tableA
where not exists (select * from tableA where tableA.id = tableB.id)";
cmd.ExecuteNonQuery();
cn.Close();
}
The SQLs used may not be right, because from your definition, it is not clear what you are really trying to do.
And BTW, you are not telling about the version, if Foxpro tables mean VFP tables then there is autoinc int field (but not necessary you can always have your own getNextId function as in the sample).
Related
I was thinking is there a query that can select many table at once?. Not using a JOINS. I tried to research about it but all I got is
SELECT * FROM Table WHERE ID IN (parameter1,parameter2);
I tried this kind of query
select * from Table Table1,Table2
but the result is getting from the Table1 only.
All tables are not it relation with each other so it means they are different table to each. What I am trying to do here is I will check the tables if there is a data on it or not. What I was using is query it all one-by-one using
select * from Table
again and again. Hope this will explain it.
For combined Tables as an output:
UNION
To allow duplicate values, use:
UNION ALL
SELECT * FROM table1
UNION
SELECT * FROM table2
For separated Tables as an output:
DataSet
DataSet dataSet = new DataSet();
DataTable table1 = new DataTable("table1");
DataTable table2 = new DataTable("table2");
dataSet.Tables.Add(table1);
dataSet.Tables.Add(table2);
using(SqlCommand cmd = new SqlCommand("SELECT * FROM table1;SELECT * from table2", con))
{
using(SqlDataReader dr = cmd.ExecuteReader())
{
dataSet.Load(dr, LoadOption.OverwriteChanges, table1, table2);
}
}
Now you can work with the DataSet's Tables like this (msdn documentation):
private void PrintRows(DataSet dataSet)
{
// For each table in the DataSet, print the row values.
foreach(DataTable table in dataSet.Tables)
{
foreach(DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
Console.WriteLine(row[column]);
}
}
}
}
Take a look at SQL command UNION. Then you can make something like
SELECT * FROM table1
UNION
SELECT * FROM table2;
I'm trying to create a query with a IN clause that maps a table with other that have a composite key. For reference the first table maps a composite key (three columns) to OtherTable (contains a OtherTableId object).
Example:
select t1
from Table t1
where t1.otherTable in :listOfOtherTables;
...
List<OtherTable> listOfOtherTables= Arrays.asList(new OtherTable(...), ...);
query.setParameter("listOfOtherTables", listOfOtherTables);
I searched how to do this and is pretty straightforward. In fact I use this with strings with success like this:
select t1
from Table t1
where t1.states in :listOfStates;
...
List<String> listOfStates = Arrays.asList("A", "B");
query.setParameter("listOfStates", listOfStates);
When I run the first example (by the way the project is in Spring 4.2 + Weblogic 12.1.2), the transformation for the query to be executed in Oracle is this:
select t1
from Table t1
where (NULL, NULL NULL) IN (("value1", "value2", "value3"), ("value1", "value2", "value3), ...);
Where is supposed to be the column names, appears NULL.
Anyone can help me?
PS: I have tried this also:
select t1
from Table t1
where t1.otherTable in (:otherTableObject1, :otherTableObject2, otherTableObject3);
Also doesn't work.
I've found that Access doesn't allow Insert Into queries for multiple records unless using a Select query from a one row table. Still, it doesn't work and I'm not sure what I'm missing.
I'm trying to insert into tblTarget, which contains 3 fields: ID, F1, F2 (Respectively auto number, number, number).
I have created a table onerow. I'm not exactly sure how it should be created, so right now it has the fields: ID, F1, F2 (Identical fields to the target table), with only one row (Empty row, except for the ID). Note that I haven't programmed the table to allow only one row, it just has only one at the moment.
The query:
INSERT INTO tblTarget (F1, F2)
SELECT * FROM (
SELECT '31','3' FROM onerow
UNION ALL
SELECT '31','2' FROM onerow);
Error I receive: "The Insert Into statement contains the following unknown field name: 'Expr1000'. Make sure you have typed correctly, and try the operation again.
My instincts say the problem is with the onerow table, but I can't figure out the solution.
This works for me in Access 2010:
INSERT INTO tblTarget (F1, F2)
SELECT *
FROM
(
SELECT 31 AS F1, 3 AS F2 FROM onerow
UNION ALL
SELECT 31, 2 FROM onerow
)
The trick was to specify the field names in the first of the UNION-ed queries so the names matched those in the INSERT clause.
try like this:
Insert into UserData(UserName,Password)
Select 'user1','abc'
Union All
Select 'user2','abc'
Forgive me as I'm relatively new to SQL. But I'm trying to take modified data from a SELECT query on one table, and use it to populate data into another table.
SELECT ID FROM TABLE WHERE VALUE=10
I want to insert the resultant ID's into another Table, but with modifications such that the value is:
1stString.ID.2ndString
I've found answers for how to use the ID from the SELECT in the insert, but I can't seem to get it working when trying to concatenate. There are also other values that I'm inserting, but they're literals (I'm trying to initialize default key settings for the ID's given in another table.
Using MS SQL Studio 2008, btw.
INSERT INTO table (field)
SELECT '1stString.' + cast(id as varchar(50)) + '.2ndString'
FROM table2
WHERE id = 10
Edit - response to comment:
You're on the right track, but you want to select your hard-coded strings from your table, like this:
INSERT INTO table1 (field1, field2, field3)
SELECT '1stVal', '2ndVal', '1stString.' + cast(id as varchar(50)) + '.2ndString'
FROM table2
WHERE id = 10
This is also illustrated in Dustin Laine's answer.
INSERT INTO table1
(
f1, f2, f3, f4
)
SELECT f1, f2, f3, 'defaultval' as f4
FROM table2
WHERE value = 1
How can I insert data using the select statement...
I have come across a question asking, what is select * from orders; used for?
And the answer was for viewing and inserting ....
Please provide an explanation ...
The answer "for viewing and inserting" is misleading and incomplete; SELECT returns data, which can then be used in almost any SQL statement.
For most normal use cases, you insert data using an INSERT statement.
INSERT INTO MyTable (MyColumn)
VALUES ('MyValue')
In some cases you may want to insert data that you are pulling from another table (or view), in which case you might use this syntax:
INSERT INTO MyTable (MyColumn)
SELECT MyColumn
FROM MyTableOrMyOtherTableOrView
Note that the exact syntax may vary depending upon your database platform.
To insert values based on a select use this syntax:
insert into table1 (mycol)
select mycol from table2
You can also use select into that enables you to create a new table : select into
And select * from table1 means select every column from table1
For a full tutorial on SQL try W3schools
You use
insert into tableX(columnsX) select columnsY from tableY
to insert data from tableY to tableX. They can both be the same. columnsY is a list of columns that exists in tableY and it must match column types in tableY.
That basically means that you'll be inserting existing data into tableX, and that existing data comes from tableY.
For the question,
"what is "select * from orders ;" used for?"
the answer
for viewing and inserting
is not correct. It is only for viewing, not inserting.
private void cnd()
{
SqlConnection con = new SqlConnection("Data Source = serverName;User ID = serverId;Password = serverPassword;Initial Catalog = DatabaseName");
con.Open();
SqlDataAdapter dp = new SqlDataAdapter("SELECT * FROM tableName", con);
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(dp);
DataSet ds = new DataSet("tableName");
dp.Fill(ds, "tableName");
DataRow dr = ds.Tables["tableName"].NewRow();
dr["columnName"] = "Value";
dr["columnName"] = "Value";
ds.Tables["tableName"].Rows.Add(dr);
int re = dp.Update(ds, "tableName");
Console.WriteLine(re.ToString());
con.Close();
}