Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
emersion
Starting Member
5 Posts |
Posted - 2008-09-04 : 08:54:14
|
Hello FriendsI want to write a procedure in which i should be able to pass a DB name as parameterand it should copy data of table to table of another databaseData From DB1.Table to DB2.TableDB1 Can be current DB so only DB2 name can be passed as Parameter.Thnx in AdvanceEmersioN |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-04 : 09:13:16
|
insert into db1.dbo.table(columns)EXEC('select columns from '+@DB2+'.dbo.table')MadhivananFailing to plan is Planning to fail |
|
|
emersion
Starting Member
5 Posts |
Posted - 2008-09-05 : 00:26:51
|
quote: Originally posted by madhivanan insert into db1.dbo.table(columns)EXEC('select columns from '+@DB2+'.dbo.table')MadhivananFailing to plan is Planning to fail
That's Nice...I am trying to execute such procedure from C#.Net and connecting to db1 via ADO.net Connection Object,But this is giving error Invalid Object / Object Not found "value of @DB2"...EmersioN |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-05 : 03:05:57
|
You need to supply value to the parameter @DB2MadhivananFailing to plan is Planning to fail |
|
|
emersion
Starting Member
5 Posts |
Posted - 2008-09-05 : 05:31:07
|
I have supplied it...but its giving error as mentioned previously.EmersioN |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-05 : 06:29:19
|
Can you post the code you used?MadhivananFailing to plan is Planning to fail |
|
|
emersion
Starting Member
5 Posts |
Posted - 2008-09-05 : 07:38:52
|
CREATE procedure [dbo].[CopyData]@DB varchar(200)as beginif @DB <>'' begin exec EmptyDatabase exec( 'insert into Medicine select * from ' + @DB + 'dbo.Medicine')endendand I do call this Procedure from C# / ASP.Net Application as usual EmersioN |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-05 : 09:13:39
|
That should beCREATE procedure [dbo].[CopyData]@DB varchar(200)asbeginif @DB <>''beginexec EmptyDatabaseexec( 'insert into Medicine select * from ' + @DB + '.dbo.Medicine')endendAlso, how did you call the procedure from .NET?MadhivananFailing to plan is Planning to fail |
|
|
emersion
Starting Member
5 Posts |
Posted - 2008-09-08 : 01:09:31
|
string archiveConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString_Archive"].ConnectionString.Replace("|ARCHIVEDBFILE|", folder + @"\" + dbName + ".mdf").Replace("|DBNAME|", dbName); lblErrorMessage.Text = "Archive Connection string : " + archiveConnectionString + "<br/>"; conArchiveDB = new SqlConnection(archiveConnectionString); conArchiveDB.Open(); transactionArchive = conArchiveDB.BeginTransaction(); cmdArchiveDB = conArchiveDB.CreateCommand(); //new SqlCommand("CopyData", conArchiveDB); cmdArchiveDB.Transaction = transactionArchive; cmdArchiveDB.CommandText = "CopyData"; cmdArchiveDB.CommandType = CommandType.StoredProcedure; cmdArchiveDB.Parameters.Add(new SqlParameter("@DB", "DataBase2"); cmdArchiveDB.ExecuteNonQuery();EmersioN |
|
|
|
|
|