all groups > sql server programming > june 2007 >
You're in the sql server programming group:
HOWTO: Copy a table between 2 SQL servers... where I have different log-in names/passwords
sql server programming:
I'm trying to copy a table between 2 different SQL Server 2000 machines. I want to do this copy with a Windows "scheduled task"... running a batch file... each midnight. (I assume oSql or BCP will be needed.) How can I do this 'table copy' when my SQL log-in names are different? I need to copy between different databases, on different servers, with different log-in names/passwords, etc) This "almost" works (except that I have different user-names and passwords on the 2 servers): SELECT * INTO ServerName2.DatabaseName2.dbo.TableName2 FROM ServerName1.DatabaseName1.dbo.TableName1 Thanks
"A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: [quoted text, click to view] > I'm trying to copy a table between 2 different SQL Server 2000 machines. > > I want to do this copy with a Windows "scheduled task"... running a batch > file... each midnight. > (I assume oSql or BCP will be needed.) > > How can I do this 'table copy' when my SQL log-in names are different? > I need to copy between different databases, on different servers, with > different log-in names/passwords, etc) > > This "almost" works (except that I have different user-names and passwords > on the 2 servers): > > SELECT * INTO > ServerName2.DatabaseName2.dbo.TableName2 > FROM > ServerName1.DatabaseName1.dbo.TableName1
You can use sp_addlinkedsvr to map your login on one server to a login on the other. But does the table definition really change every day? Can't you keep the table definition and just copy the data? In such case you can use BCP to bulk out the data from the source server, and then load it into the target server. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at
Hello, The following example creates a mapping to make sure that the SQL server login Finance connects through to the linked server Accounts by using the login populateuser and password password123. EXEC sp_addlinkedsrvlogin 'Accounts', 'false', 'finance, populateuser, password123After this u can use linked server. But I recommend to user a DTS package to move table and database and schedule it using SQL Server [quoted text, click to view] Agent.THanksHari""A_Michigan_User"" <Michigan_RE_M0VE@ameritech.net> wrote in message news:ejT4g$sqHHA.3248@TK2MSFTNGP03.phx.gbl... > I'm trying to copy a table between 2 different SQL Server 2000 machines. > > I want to do this copy with a Windows "scheduled task"... running a batch > file... each midnight. > (I assume oSql or BCP will be needed.) > > How can I do this 'table copy' when my SQL log-in names are different? > I need to copy between different databases, on different servers, with > different log-in names/passwords, etc) > > This "almost" works (except that I have different user-names and passwords > on the 2 servers): > > SELECT * INTO > ServerName2.DatabaseName2.dbo.TableName2 > FROM > ServerName1.DatabaseName1.dbo.TableName1 > > Thanks > >
I'll try that link-command. (I think I misunderstood what "linking" does. I kept thinking that I do not want to *LINK* these 2 tables together... or linking the servers together... in any way. But I guess that's not really what 'linking' does.) [quoted text, click to view] > But I recommend to user a DTS package to move table and database and > schedule it using SQL Server Agent.
Really? Loading/running a huge GUI/DTS package (which we never use otherwise)... and then moving the entire database (even though we only need this 1 small table copied)... and then keep the Agent running all the time... for this 1 event that happens once per day? [quoted text, click to view] > The following example creates a mapping to make sure that the SQL server > login Finance connects through to the linked server Accounts by using the > login populateuser and password password123. > > EXEC sp_addlinkedsrvlogin 'Accounts', 'false', 'finance, populateuser, > password123After this u can use linked server. But I recommend to user a > DTS package to move table and database and schedule it using SQL Server > Agent. THanksHari""A_Michigan_User"" <Michigan_RE_M0VE@ameritech.net> wrote > in message news:ejT4g$sqHHA.3248@TK2MSFTNGP03.phx.gbl... >> I'm trying to copy a table between 2 different SQL Server 2000 machines. >> >> I want to do this copy with a Windows "scheduled task"... running a batch >> file... each midnight. >> (I assume oSql or BCP will be needed.) >> >> How can I do this 'table copy' when my SQL log-in names are different? >> I need to copy between different databases, on different servers, with >> different log-in names/passwords, etc) >> >> This "almost" works (except that I have different user-names and >> passwords on the 2 servers): >> >> SELECT * INTO >> ServerName2.DatabaseName2.dbo.TableName2 >> FROM >> ServerName1.DatabaseName1.dbo.TableName1 >> >> Thanks >> >> > >
[quoted text, click to view] > But does the table definition really change every day? Can't you keep > the table definition and just copy the data? In such case you can use > BCP to bulk out the data from the source server, and then load it into > the target server.
The table-definition might change every few YEARS. The data changes hourly... and we need to copy that 1 table... daily. (About 4000 records.) I'm not sure which would be quicker or more efficient: 1> Dropping the entire table in 1 hit. 1> Select/insert/into... all the new data. 2> Dropping every row of the data. (Keeping the table.) 2> Bulk copy all the new rows in I'll give it a try. Thanks! [quoted text, click to view] "Erland Sommarskog" <esquel@sommarskog.se> wrote in message news:Xns994B3F39E24Yazorman@127.0.0.1... > "A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: >> I'm trying to copy a table between 2 different SQL Server 2000 machines. >> >> I want to do this copy with a Windows "scheduled task"... running a batch >> file... each midnight. >> (I assume oSql or BCP will be needed.) >> >> How can I do this 'table copy' when my SQL log-in names are different? >> I need to copy between different databases, on different servers, with >> different log-in names/passwords, etc) >> >> This "almost" works (except that I have different user-names and >> passwords >> on the 2 servers): >> >> SELECT * INTO >> ServerName2.DatabaseName2.dbo.TableName2 >> FROM >> ServerName1.DatabaseName1.dbo.TableName1 > > You can use sp_addlinkedsvr to map your login on one server to a login > on the other. > > But does the table definition really change every day? Can't you keep > the table definition and just copy the data? In such case you can use > BCP to bulk out the data from the source server, and then load it into > the target server. > > > -- > Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Wow. Working with sp_addlinkedsrvlogin is rougher than I thought [quoted text, click to view] > EXEC sp_addlinkedsrvlogin 'Accounts', 'false', 'finance', 'populateuser', > 'password123'
One server is: Named: ServerName1 I can only reach it via its IP address 11.11.11.11 My name there is User1 and my password is Password1 The other server is: Named: ServerName2 I can only reach it via its IP address 22.22.22.22 My name there is User2 and my password is Password2 How does that fit into sp_addlinkedsrvlogin ???? [quoted text, click to view] > BCP to bulk out the data from the source server, and then load it into the > target server.
I assume I'll have to use sp_addlinkedsrvlogin with BCP... as I would with my original SELECT/INTO cmd? Thanks for all the help. [quoted text, click to view] "Erland Sommarskog" <esquel@sommarskog.se> wrote in message news:Xns994B3F39E24Yazorman@127.0.0.1... > "A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: >> I'm trying to copy a table between 2 different SQL Server 2000 machines. >> >> I want to do this copy with a Windows "scheduled task"... running a batch >> file... each midnight. >> (I assume oSql or BCP will be needed.) >> >> How can I do this 'table copy' when my SQL log-in names are different? >> I need to copy between different databases, on different servers, with >> different log-in names/passwords, etc) >> >> This "almost" works (except that I have different user-names and >> passwords >> on the 2 servers): >> >> SELECT * INTO >> ServerName2.DatabaseName2.dbo.TableName2 >> FROM >> ServerName1.DatabaseName1.dbo.TableName1 > > You can use sp_addlinkedsvr to map your login on one server to a login > on the other. > > But does the table definition really change every day? Can't you keep > the table definition and just copy the data? In such case you can use > BCP to bulk out the data from the source server, and then load it into > the target server. > > > -- > Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Trying to use only BCP: bcp db1.dbo.T1 out 1.bcp -S server1 -U user1 -P pw1 -n bcp db2.dbo.T2 in 1.bcp -S server2 -U user2 -P pw2 -n The first line works... but the second line gives a series of errors: SQLState = 37000, NativeError = 170 Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'char, datetime, etc'. I'm getting closer and closer to a solution. [quoted text, click to view] "Erland Sommarskog" <esquel@sommarskog.se> wrote in message news:Xns994B3F39E24Yazorman@127.0.0.1... > "A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: >> I'm trying to copy a table between 2 different SQL Server 2000 machines. >> >> I want to do this copy with a Windows "scheduled task"... running a batch >> file... each midnight. >> (I assume oSql or BCP will be needed.) >> >> How can I do this 'table copy' when my SQL log-in names are different? >> I need to copy between different databases, on different servers, with >> different log-in names/passwords, etc) >> >> This "almost" works (except that I have different user-names and >> passwords >> on the 2 servers): >> >> SELECT * INTO >> ServerName2.DatabaseName2.dbo.TableName2 >> FROM >> ServerName1.DatabaseName1.dbo.TableName1 > > You can use sp_addlinkedsvr to map your login on one server to a login > on the other. > > But does the table definition really change every day? Can't you keep > the table definition and just copy the data? In such case you can use > BCP to bulk out the data from the source server, and then load it into > the target server. > > > -- > Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
"A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: [quoted text, click to view] > Working with sp_addlinkedsrvlogin is rougher than I thought
Linked servers often means hassle. [quoted text, click to view] >> EXEC sp_addlinkedsrvlogin 'Accounts', 'false', 'finance', 'populateuser', >> 'password123' > > One server is: > Named: ServerName1 > I can only reach it via its IP address 11.11.11.11 > My name there is User1 and my password is Password1 > > The other server is: > Named: ServerName2 > I can only reach it via its IP address 22.22.22.22 > My name there is User2 and my password is Password2 > > How does that fit into sp_addlinkedsrvlogin ????
Let's say you set up the linked server Server2 on Server1. Then this should work, I think: sp_addlinkedsrvlogin 'ServerName1', 'false', 'User1', 'User2', Password2' [quoted text, click to view] > I assume I'll have to use sp_addlinkedsrvlogin with BCP... as I would with > my original SELECT/INTO cmd?
As you probably already found out, no you don't. You connect to one server at a time. [quoted text, click to view] > Trying to use only BCP: > > bcp db1.dbo.T1 out 1.bcp -S server1 -U user1 -P pw1 -n > bcp db2.dbo.T2 in 1.bcp -S server2 -U user2 -P pw2 -n > > The first line works... but the second line gives a series of errors: > > SQLState = 37000, NativeError = 170 > Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect > syntax near 'char, datetime, etc'.
Eh, interesting. I don't know what that could mean. Have you checked that the table defintion is the same on both servers? Which version of SQL Server are they running? -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at
Both servers are MS SQL Server 2000. I can't even get my 2 lines of BCP code to work... even on the *SAME* server, same database, same user, same password. (I still get that long list of errors.) The table defitions are identical. (For my test... I'm just using an exact copy of the same table.) I drop the 2nd table... then copy the 1st table to the 2nd... then I try to run my 2 lines of BCP and... a long list of errors appear. Seems like I can't get BCP to even do the simpliest of tasks. [quoted text, click to view] >> bcp db1.dbo.T1 out 1.bcp -S server1 -U user1 -P pw1 -n >> bcp db2.dbo.T2 in 1.bcp -S server2 -U user2 -P pw2 -n >> >> The first line works... but the second line gives a series of errors: >> >> SQLState = 37000, NativeError = 170 >> Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect >> syntax near 'char, datetime, etc'. > > Eh, interesting. I don't know what that could mean. Have you checked > that the table defintion is the same on both servers? Which version of > SQL Server are they running?
Wow... I'm getting this error only when I try to BCP certain tables (but not others). Something about a strange assortment of int/varchar/char/datetime fields in certain tables. (Some fields allowing NULLS... and other fields not.) I'll have to try deleting certain fields... and see where BCP starts to work ok. I'll post the table defs when I find out. [quoted text, click to view] > bcp db1.dbo.T1 out 1.bcp -S server1 -U user1 -P pw1 -n > bcp db2.dbo.T2 in 1.bcp -S server2 -U user2 -P pw2 -n > > The first line works... but the second line gives a series of errors: > > SQLState = 37000, NativeError = 170 > Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect > syntax near 'char, datetime, etc'. > > I'm getting closer and closer to a solution. > > > "Erland Sommarskog" <esquel@sommarskog.se> wrote in message > news:Xns994B3F39E24Yazorman@127.0.0.1... >> "A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: >>> I'm trying to copy a table between 2 different SQL Server 2000 machines. >>> >>> I want to do this copy with a Windows "scheduled task"... running a >>> batch >>> file... each midnight. >>> (I assume oSql or BCP will be needed.) >>> >>> How can I do this 'table copy' when my SQL log-in names are different? >>> I need to copy between different databases, on different servers, with >>> different log-in names/passwords, etc) >>> >>> This "almost" works (except that I have different user-names and >>> passwords >>> on the 2 servers): >>> >>> SELECT * INTO >>> ServerName2.DatabaseName2.dbo.TableName2 >>> FROM >>> ServerName1.DatabaseName1.dbo.TableName1 >> >> You can use sp_addlinkedsvr to map your login on one server to a login >> on the other. >> >> But does the table definition really change every day? Can't you keep >> the table definition and just copy the data? In such case you can use >> BCP to bulk out the data from the source server, and then load it into >> the target server. >> >> >> -- >> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se >> >> Books Online for SQL Server 2005 at >> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx >> Books Online for SQL Server 2000 at >> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx > >
"A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: [quoted text, click to view] > I drop the 2nd table... then copy the 1st table to the 2nd...
How do you perform this? (I still think that you should not care about copying the table structure. But you can use TRUNCATE TABLE to empty the target table before you run BCP.) [quoted text, click to view] > then I try to run my 2 lines of BCP and... a long list of errors appear.
Could you post these error messages? -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at
DROP TABLE dbo.T1 GO CREATE TABLE dbo.T1 ( [field one] char(32) NULL, [field two] datetime NULL ) GO INSERT dbo.T1 VALUES('a', '12-31-2007') === [quoted text, click to view] > bcp db1.dbo.T1 out 1.bcp -S Server1 -U user1 -P pword1 -n > bcp db1.dbo.T1 in 1.bcp -S Server1 -U user1 -P pword1 -n
=== SQLState = 37000, NativeError = 170 Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'char'. (You'll get a long list of errors... based on how many of your fields have spaces in their names.) === Using Sql Server 2000. I drop/create/insert with QA. I run BCP from a batch-file script on a remote Windows XP machine. Seems like BCP doesn't like my field-names to have spaces in them. (That's part of a table that's already in use... and I can't possibly change it now.) When I remove the spaces... everything else seems to work. Is this a bug in BCP???? [quoted text, click to view] "Erland Sommarskog" <esquel@sommarskog.se> wrote in message news:Xns994EB75799EYazorman@127.0.0.1... > "A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: >> I drop the 2nd table... then copy the 1st table to the 2nd... > > How do you perform this? (I still think that you should not care about > copying the table structure. But you can use TRUNCATE TABLE to empty the > target table before you run BCP.) > >> then I try to run my 2 lines of BCP and... a long list of errors appear. > > Could you post these error messages?
"A_Michigan_User" (Michigan_RE_M0VE@ameritech.net) writes: [quoted text, click to view] > DROP TABLE dbo.T1 > GO > > CREATE TABLE dbo.T1 > ( > [field one] char(32) NULL, > [field two] datetime NULL > ) > GO > > INSERT dbo.T1 VALUES('a', '12-31-2007') > >=== > >> bcp db1.dbo.T1 out 1.bcp -S Server1 -U user1 -P pword1 -n >> bcp db1.dbo.T1 in 1.bcp -S Server1 -U user1 -P pword1 -n > >=== > > SQLState = 37000, NativeError = 170 > Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect > syntax near 'char'. > (You'll get a long list of errors... based on how many of your fields > have spaces in their names.)
It seems that the problem is with the spaces in the column name. BCP fails to quote the names. In Profiler, I can see that it generates the statement: insert bulk tempdb.dbo.T1(field one char(32) COLLATE Finnish_Swedish_CS_AS,field two datetime) Which is incorrect syntax. When I tested this with the BCP that ships with SQL 2005, the problem did not occur. Would it be possible for you to have spaceless names in the target table? If nothing else, you could have a view with spaceless names over the table, and bulk into the view. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at
Don't see what you're looking for? Try a search.
|
|
|