Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have a database in sql server 2005
i have one table called "main_table", it has number of data of students
i have another table of same design, in same database, it is called "student_table"

i want, when i click a button in c#, the data from "main_table" would be copy to "student_table" with a condition "where year= 2005 (or any i specify)"

i have further results to calculate from "students_table" so i need that data, not all the data

can anyone help
Posted

You can have a look at Insert Into with Select option.

e.g.
SQL
INSERT INTO Table1 (A, B)
SELECT A,B
FROM Table2
WHERE column > [condition]


More details here[^].
 
Share this answer
 
Comments
OriginalGriff 27-Aug-11 3:41am    
The only thing I really don't like about that is the effect in a multiuser environment...
Why create another table, which holds the results? Instead, pass the results of a query to another query (this is called Nested Queries) http://www.sql-tutorial.com/sql-nested-queries-sql-tutorial/[^]

[edit]One of the reasons not to do what you propose is that it won't work at all in a multiuser environment: Who last put the data into the table? Was it you? Or someone half the world away? You don't know, and you can't assume it was you.

If you must put it into a table, then look at Temporary Tables, and do it in a stored procedure.

But by preference, don't do it at all, unless you really have to.[/edit]
 
Share this answer
 
v2
Comments
Db issues 29-Aug-11 0:44am    
You are exactly right, i should send condition in a query and than select only that data for every report, but this is phase 1, in phase 2, i will be working with that approach, i have that aspect in mind too and i do consult again for that aspect, highly thanks
You can use SQL query as below for insertion.

INSERT INTO student_table(Col1,Col2,Col3) SELECT Col1,Col2,Col3 FROM main_table where year= 2005;

And have a look at below link for Ado.Net Data Insertion.

http://csharp.net-informations.com/data-providers/csharp-sqlcommand-executenonquery.htm[^]
 
Share this answer
 
Comments
Db issues 29-Aug-11 0:42am    
Thanks
query u need to write in button click event
INSERT INTO student table (column names)SELECT (column names ) FROM maintable where condition
 
Share this answer
 
v3

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