Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
actually i am developing a project for a company.so my client wants that all the transaction of today date copy to main database that has all the transactions.
and don't want to share his all transaction to others expect today date .

so i want copy the data of today date to main database that has same tables day by day.
so plz help me as soon as possible....
thanks
Posted
Updated 1-Feb-17 12:42pm
v2

Hi,

try like this,,
SQL
SELECT * INTO Table2 FROM Table1 WHERE Date = GETDATE()

refer this for more
http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/[^]

hope it works.
 
Share this answer
 
v2
Comments
mayank1290 16-Jul-12 7:01am    
first of all thanks for reply ....
but i want to transfer data into another database's table.another database is also exist into same project.i have used ...

SqlCommand cmd = new SqlCommand("Insert Into Db2.mdf..student (name, address,date)Select name, address,date From Db1.mdf..student", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
but it give the error--
Could not find server 'Db1' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

so plz give me solution..
Karthik Harve 16-Jul-12 7:08am    
does the two databases are in same server..??
if yes, try like this..

SELECT * INTO Db1.dbo.Table2 FROM Db2.dbo.Table1 WHERE [CONDITION]

if No, i think you cannot execute in a single query with single connection
mayank1290 16-Jul-12 7:16am    
@karthik
i have use but it give the error---Invalid object name 'Db1.dbo.student'.

my code is this--
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Order"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT * INTO Db2.dbo.student FROM Db1.dbo.student", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();


}
}
Karthik Harve 16-Jul-12 7:31am    
"Db1" i just used for sample. replace with your actual database name. in positions of both db1 and db2 use your database names.
mayank1290 18-Jul-12 2:21am    
@karthik i have two database one is Db1 another is Db2.
i want to copy of data of Db1's table student into Db2's table student on Button Click event....


so please give me solution...
Hi,

to insert todays records in another table try following query
create strored procedure for this Query
SQL
Declare vchrScript as nvarchar(max)
set vchrScript='insert into Db2.dbo.table1(col1,col2,col3)(select col1,col2,col3 from Db1.dbo.table1 where colDate=getdate())'
Execute sp_executesql @vchrScript


hope it help u
Best Luck
 
Share this answer
 
v3
Comments
mayank1290 16-Jul-12 7:05am    
first of all thanks for reply ....
but i want to transfer data into another database's table.another database is also exist into same project.i have used ...

SqlCommand cmd = new SqlCommand("Insert Into Db2.mdf..student (name, address,date)Select name, address,date From Db1.mdf..student", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
but it give the error--
Could not find server 'Db1' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

so plz give me solution..
Nilesh Patil Kolhapur 16-Jul-12 7:47am    
u have to change connectionstring to do this.
Karthik Harve's ans create new table every time check it out
mayank1290 18-Jul-12 2:26am    
i have two database one is Db1 another is Db2.
i want to copy of data of Db1's table student into Db2's table student on Button Click event....


so please give me solution...
Nilesh Patil Kolhapur 18-Jul-12 3:49am    
try this
insert into dbo.Db2.table2(col1,col2,col3)(select col1,col2,col3 from dbo.Db1.table1 where colDate=getdate())
mayank1290 18-Jul-12 7:49am    
actually my database name is Db1.mdf and Db2.mdf and both have same table names.and server is also same because i am using in same project.
but every time i get same error-"Invalid object Db1.dbo.student"
Guys,
I was trying this for hours finally found the solution for this,
please use sa credentials in your connection string or use a common user credential for both databases.
 
Share this answer
 
Comments
[no name] 1-Feb-17 18:49pm    
No! Never, ever use the sa account!
Dave Kreskowiak 1-Feb-17 19:22pm    
Don't add an answer to a FIVE YEAR OLD question that was already answered.

On top of that, you're answer isn't an answer at all. You NEVER EVER IN YOUR LIFE use the SA account for anything other than installing the SQL server. After that, it's not to be used for anything at all, well, unless you want to risk destroying the server with it.

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