Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have table(say Table_A) with 2 Records, and another table (Say Table_B) which is having reference (Foreign key) from Table_A. I Wanted to Insert a New Record in between 2 Records of Table_A.

Each time when I insert new records I wanted to truncate and insert all records of Table_A and Table_B which Should keep all references as it is with newly inserted record in between 2 old record.

I am importing records for Table_A and from Excel sheet, but Table_B data is inserting from fornt End.

Thanks in Advance..
Posted
Comments
[no name] 22-Jun-13 14:26pm    
Why would you think that you would need to do this at all? The only time that the order of your rows matter in when you query them. Other than that, it makes no difference at all.
GauravThorat 23-Jun-13 13:55pm    
Before Importing Data from Excel:

Table_A Table_B
Aid Aval Bid Bval
1 abc 1 2
2 pqr 2 2

and After Importing Data from Excel I required the below:

Table_A Table_B
Aid Aval Bid Bval
1 abc 1 2
2 xyz 2 3
3 pqr

I am getting values for Table_A as per requirement but I wanted to update Table_B values properly..


Thanks

1 solution

Rows in an SQL database are not like rows in Excel, where they are positionally related to each other. Don't think of a database that way, as ThePhantomUpvoter said in the comment, row position doesn't matter. Most (if not all) SQL databases don't have a method to insert rows between or before, they are always appended. This is because it would have to change all the records in the database to update/insert one. This would be a very expensive operation.

In order to get the data in the order you want, look up how to use ORDER BY in your SQL select statements. Depending on the database back-end you use, you can do interesting JOIN's and form what you are looking for in the order you need it displayed.
 
Share this answer
 
Comments
GauravThorat 23-Jun-13 13:55pm    
Before Importing Data from Excel:

Table_A Table_B
Aid Aval Bid Bval
1 abc 1 2
2 pqr 2 2

and After Importing Data from Excel I required the below:

Table_A Table_B
Aid Aval Bid Bval
1 abc 1 2
2 xyz 2 3
3 pqr

I am getting values for Table_A as per requirement but I wanted to update Table_B values properly..


Thanks
RedDk 23-Jun-13 14:26pm    
Neither R or T is exactly addressing the problem when they tell you SQL is this or SQL is that because language is only a barrier to "saying" what you want to say. But looking at the Table_A value change after "Import" ... is this a mistake in copy/paste?
GauravThorat 25-Jun-13 8:31am    
No this is not mistake in copy paste as Aid is primary key it will change from 2 -> 3 for pqr

and I still not get my answer....Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900