Copy data from a table to another one has different structure - migrate

I have two tables with different structure:
TableSource:
intId(not null), txtSummary, strDetail
TableDesc:
guidId(not null), guidFK(not null), Name, Detail
I want to migrate data from two fields of tableSource(txtSummary, strDetail) to
two fields of tableDesc(Name, Detail).
guidId is auto generation and guidFK should be assigned to a fixed value.
I'v tried writing some t-sql code lines but not success. Could anyone help me?

Something like:
INSERT into TableDesc (Name, Detail)
SELECT txtSummary, strDetail
FROM TableSource
should work, if the other fields are auto-generated on insert like you said.
Which database is this using?

Related

Phalcon Save: Can I update only specified columns if update?

I have a table with this columns:
param_id
val_value
val_flag
date_value
tmp_value
tmp_flag
And I have an array of elements, I always have param_id and date_value, but sometimes i have val columns and other times I have tmp columns.
I use $data->save() because sometimes I need to create a new register in db and other times I need to update the register with param_id and date_value.
The question is: Is there any way to do an insert/update but when is an update, only update tmp columns or val columns? I think a find First is my only option, but maybe there is another way.
Thank you.
[EDIT]
I'm trying with the whitelist but it does not work. Let me explain how the method works: I get a request to a web service, process the xml and generate an array of elements with the information collected, after processing these elements I have an array of elements of the class appropriate to save, but these may be new or existing and may contain tmp or val values, I have tried with this but I still change the values to null.
if ($medida->tipo == 'temporal'){
$whiteList = array('val_value','val_flag');
}else if ($medida->tipo == 'validado'){
$whiteList = array('tmp_value','tmp_flag');
}
$dato->save(null, $whitelist);
I do not have data of the post, I use null instead, I have also tried to use an array with the manual assignment of the data obtaining the same result.
Here are two options that can help you:
1) Use 'whitelist' for the save() method.
$obj->save($this->request->getPost(), ['tmp_1', 'tmp_2']);
More info in the documentation.
2) Use the 'Event Manager'.
The methods beforeCreate() and beforeUpdate() will be useful so you can decide which fields to use.
More info in the documentation.
Also if you really want phalcon phql to update only columns which changed you need to enable dynamic update.

vaadin7 getting values from container

Currently I load data from database to Table in vaadin7 by implementing LazyQueryContainer.
Here is my code:
final LazyQueryContainer container = new LazyQueryContainer(factory, /* index */null, /* batchSize */50, false);
for (Column c : columns) {
container.addContainerProperty(c.getCaption(), c.getType(), null, false, true);
}
table.setContainerDataSource(container);
It works as expected.
However I want to display the data not in table format but as a chart. I have already implemented Jfreechart so all I need to do is somehow retrieve the data from the container (or maybe from the table?)
I can iterate over container.getContainerPropertyIds() but I am not sure whether it is the right thing to do.
How can I get the content of one column of the table? For example the first column in my table is called "date" and I would like to get all date values in a list, or array,etc.
Any help is appreciated.

Cannot insert new Employee entity using InsertOnSubmit()

I'm facing this exception An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported. when I try to insert a new entity into my Employees table (the master one).
There is a relationship between the master Employees table and the details Orders table, and I'm sure that the relationship between these two tables (and specifically Employee.Orders EntitySet) is the cause of the problem since when I removed the relationship, it returns back to insert into Employees table with no problems.
When I searched for the problem, there was this blog post which I tried to implement but my case is a different than the one in the blog post in these items:
He faces the exception when tries to update (while I try to insert).
The tables architecture is different.
how can I solve this problem?
Here's the insertion code:
Employee emp = new Employee();
emp.Name = empName; // empName is a local variable
// What should I default emp.Orders to?
dc.Employees.InsertOnSubmit(emp);
dc.SubmitChanges();
P.S: My DataContext is defined on class-level in my repository and the exception is being thrown when I call dc.SubmitChanges();. and I didn't Attach any object why does it say that?
Here is an article explaining what you need to do using the Attach and Detach methods:
http://www.codeproject.com/KB/linq/linq-to-sql-detach.aspx
I am guessing it is trying to save something else besides just the employee object or you aren't showing us the full code in your repository. When you instantiate your DataContext object (dc) try setting DeferredLoadingEnabled = false, and ObjectTrackingEnabled = false and see if it works. If it does, try watching the SQL code in SQL Server Profiler and see if it is modifying other objects that may have came from a different context like the message says.
var dc = new MyDataContext()
{
DeferredLoadingEnabled = false,
ObjectTrackingEnabled = false
};
My bet is on the primary key.
Are you sure the primary key is also set on auto increment?
Did you
try changing the name, does it work then?
What happens if you remove
all rows from your DB? can you insert one then?

Saving a symfony checkbox list

If you create a checkbox list in symfony 1.2 you get an array with the checked options back in the form. If you save the form, your database now contains the words "Array". Is there a way around this? Or should I just json_encode / json_decode the array as ncecessary and save it manually? Seems awfully tedious.
Thanks for reading.
You can use serialize() and unserialize() functions when saving and getting data.
I don't know which orm using but i can explain with propel way.
For example you have post table and Post class. And post table has options column with text or varchar data type.
in Post.class.php your model directory you can define two override methods
setOptions($v)
{
parent::setOptions(serialize($v));
}
getOptions()
{
return unserialize($this->options);
}
Just like that.
In your view or action you can get all options with $post->getOptions() and you have an Array that contains all option related to your database record.

Tables from 2 databases in one LINQ to SQL class

I want to include a table (Events) from another database in my LINQ to SQL class.
How should I format the Data Source of that table?
I have tried with IP.dbo.Events, IP.DatabaseName.dbo.Events and so on, but can not get it to work.
The reason for all this is to select Events that a Client have attended.
I have also tried to have the table in another LINQ to SQL class, but then it complains on the Data Context.
public static IQueryable<Event> ByClientID(this IQueryable<Event> events, int clientID)
{
events = from e in events
from c in new MyDataContext().ClientCourses
where e.EventID == c.CourseID &&
c.ClientID == clientID
select e;
return events;
}
You can only use tables that reside on the same physical SQL Server in two different instances. I did this once as someone had "cleverly" put an application's DB across two database instances.
There is a blog post on it here that may help.
Could you create a view that returns the data from the 2nd database and use this instead? (Not tried this so absolutely no idea if it'll work :)
Obviously this is no good if you need to be saving to the other database too..

Resources