0
    System.Data.SqlClient.SqlException occurred
  HResult=0x80131904
  Message=Invalid object name 'Drivers.DriversDetailTable'.
  Source=.Net SqlClient Data Provider
  StackTrace:
<Cannot evaluate the exception stack trace>

Keep getting the above error everytime i try and copy stuff from one database table to another, heres my code.

SqlConnection connect = new SqlConnection($@"Data Source = Computer\SQLEXPRESS;Initial Catalog = Drivers;Trusted_Connection=True;");
            connect.Open();
            SqlCommand cmd = new SqlCommand($@"SELECT * INTO BackUpDatabase.DriversDetailTable FROM Drivers.DriversDetailTable", connect);
            cmd.ExecuteNonQuery();
            connect.Close();
6
  • And what is the error? Tag with the database you are using. Commented Jul 7, 2017 at 11:07
  • How do i tag with the database? Sorry i'm new to this, just started it today Commented Jul 7, 2017 at 11:08
  • Seems like you should use the sql-server tag. Commented Jul 7, 2017 at 11:10
  • 1
    try this: "SELECT * INTO BackUpDatabase.dbo.DriversDetailTable FROM Drivers.dbo.DriversDetailTable". Put dbo between database and table name. Or the schema your using. Commented Jul 7, 2017 at 11:12
  • You could also use INSERT INTO ... SELECT which seems to be more common in other databases. And you should specify column names if possible. Commented Jul 7, 2017 at 11:14

1 Answer 1

3

Looks like your database name is Drivers not schema name

SELECT * 
INTO BackUpDatabase.DriversDetailTable 
FROM Drivers.DriversDetailTable --here schema name is missing

When you use two part identifer then it will be parsed as

schemaname.tablename

So Drivers and BackUpDatabase is considered as schema names instead of database.

It should be

SELECT * 
INTO BackUpDatabase.dbo.DriversDetailTable 
FROM Drivers.schemaname.DriversDetailTable --here replace schemaname with dbo or whatever schema name

Note : When you use INTO clause it creates a new table. When you run second time you may get a error stating table already exist. Better to create table in prior in backup database use INSERT INTO..select to copy the rows

Insert into BackUpDatabase.dbo.DriversDetailTable (col1,col2,..)
select col1,col2,..FROM Drivers.schemaname.DriversDetailTable
Sign up to request clarification or add additional context in comments.

10 Comments

System.Data.SqlClient.SqlException occurred HResult=0x80131904 Message=The specified schema name "BackUpDatabase" either does not exist or you do not have permission to use it. Source=.Net SqlClient Data Provider StackTrace: <Cannot evaluate the exception stack trace>
do i have to delete the database table if it already exists because i'm getting this error
System.Data.SqlClient.SqlException occurred HResult=0x80131904 Message=There is already an object named 'DriversDetailTable' in the database. Source=.Net SqlClient Data Provider StackTrace: <Cannot evaluate the exception stack trace>
@JohnDoe - Read my note at the end
Not really sure how to identify the database schema
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.