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 |
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-04-15 : 11:38:04
|
SQL Server 7 SP 3How would one form the sp_OAMethod call to use the ImportData Method. I have a SQL Server object (@oSQLServer), and a BulkCopy object (@oBCP), but I can't seem to form the call correctly...set @method = 'Databases("myDB").Tables("myTable").ImportData'exec @HRESULT = sp_OAMethod @oSQLServer, @method, @NumRows OUTPUT, @oBCP ...yields a error (interpreted through sp_OAGetErrorInfo) as source = 'Microsoft SQL-DMO' and description = '[SQL-DMO]'. Not very helpful. BOL tells me the syntax looks like ...quote: object.ImportData( BulkCopy ) as Long
... but I can't figure out how do do that with sp_OAMethod. The syntax I am using was taken from here.Here is the full code...create procedure usp_bulkcopy @sourceserver varchar(255) = null, @sourcedb varchar(255) = null, @sourcetable varchar(255) = null, @sourcefile varchar(1000) = null, @destserver varchar(255) = null, @destdb varchar(255) = null, @desttable varchar(255) = null, @destfile varchar(1000) = null, @formatfile varchar(1000) = null, @numrows int out asset nocount on/******************************************************************************* Locals******************************************************************************/declare @oSQLServer int, --SQL Server Object @oBCP int, --BulkCopy Object @oResults int, --Results Object @HRESULT int, --sp_oa* return value @verify int declare @error varchar(8000), @datafilepath varchar(1000), @exportmethod varchar(255), @importmethod varchar(255), @method varchar(255)select @error = 'unknown error', @datafilepath = coalesce(@sourcefile,@destfile), @exportmethod = 'Databases("' + @sourcedb + '").' + 'Tables("' + @sourcetable + '").' + 'ExportData', @importmethod = 'Databases("' + @destdb + '").' + 'Tables("' + @desttable + '").' + 'ImportData'--error handlerif (@datafilepath is null and (@exportmethod is null or @importmethod is null)) or (@datafilepath is not null and @formatfile is null)begin set @error = 'Illegal call to usp_bulkcopy: missing params' goto ErrorConditionend/******************************************************************************* Create the SQLServer object******************************************************************************/exec @HRESULT = sp_OACreate 'SQLDMO.SQLServer', @oSQLServer outif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionend-- Set the login process to use NT Authenticationexec @HRESULT = sp_OASetProperty @oSQLServer, 'LoginSecure', -1if @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionend-- Connect to server using NT Authenticationexec @HRESULT = sp_OAMethod @oSQLServer, 'Connect', NULL, @@SERVERNAMEif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionend-- Verify the connectionexec @HRESULT = sp_OAMethod @oSQLServer, 'VerifyConnection', @verify outif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionendif @verify = 0begin set @error = 'Unable to verify connection' goto ErrorConditionend/******************************************************************************* Create the BCP Object******************************************************************************/exec @HRESULT = sp_OACreate 'SQLDMO.BulkCopy', @oBCP OUT if @HRESULT <> 0begin exec usp_displayoaerrorinfo @oBCP, @HRESULT, @error out goto ErrorConditionend-- Set the file pathexec @HRESULT = sp_OASetProperty @oBCP, 'DataFilePath', @datafilepathif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oBCP, @HRESULT, @error out goto ErrorConditionend-- Set the format fileexec @HRESULT = sp_OASetProperty @oBCP, 'FormatFilePath', @datafilepathif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oBCP, @HRESULT, @error out goto ErrorConditionend/******************************************************************************* Do the bulk copy******************************************************************************/set @method = coalesce(@importmethod,@exportmethod)exec @HRESULT = sp_OAMethod @oSQLServer, @method, @NumRows OUTPUT, @oBCPif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionend/******************************************************************************* Destroy the objects******************************************************************************/exec @HRESULT = sp_OADestroy @oSQLServerif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionendexec @HRESULT = sp_OADestroy @oBCPif @HRESULT <> 0begin exec usp_displayoaerrorinfo @oSQLServer, @HRESULT, @error out goto ErrorConditionendreturn(0)/******************************************************************************* ErrorCondition******************************************************************************/ErrorCondition:RAISERROR(@error,16,1) WITH LOGreturn(1)go (note usp_displayoaerrorinfo is ripped right out of books on line)Thanks for you help...<O> |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-04-15 : 11:50:44
|
| Take a look at the SQL-DMO samples that are in the Tools/DevTools folder(s) in the SQL Server program file folder. You may need to unzip them. I also have a feeling that the only samples for the Bulk Copy object are in C++, and the BulkCopy interface seems to use pointers, so converting it may get tricky.What's wrong with using bcp or BULK INSERT, if you don't mind my asking? |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-04-15 : 12:17:57
|
quote: What's wrong with using bcp or BULK INSERT, if you don't mind my asking?
Well, it all starts here has a little to do with this. I am working on a process that checks for the existance of a file in a directory, once it is there, it copies the file to another directory, once the copy is done, I want to bulk insert it into a table. All the while I am using my variation on the pope/buchholz error logging outside of a transaction. I am doing all of this in stored procedures, because of the buggy-ness I was experiencing DTS error handling with Execute SQL Tasks.I have usp_waitforfile and usp_copyfile written and working as well as the usp_logaction. I had originally tried using dynamic sql to put together a bulk insert statement, however, I was getting the OLE 'STREAM' error mentioned in the above link. BCP is out, by the way, because I would like to use NT Auth (by the way, if bcp and bulk copy both work off the same api, there must be a way to bcp with nt auth, right?). Next I tried creating a SQLDMO connection and executing my dynamically build bulk insert string on a different connection. However, a) I was running up against the 255 character limit for parms on sp_OAMethod and b) when I kept the filenames short I was getting an error...quote: Server: Msg 50000, Level 16, State 1, Line 0OLE Error--HRESULT: 0x800412ff Source: Microsoft SQL-DMO (ODBC SQLState: 42000) Description: [Microsoft][ODBC SQL Server Driver][SQL Server]Bulk insert data conversion error (truncation) for row 1, column 56 (Product_Expand9).
... of course, if I PRINT @bulkinsertsqlstring and cut & paste to QA, it runs just fine.So this . . . the BULKCOPY SQL-DMO junk, is my third attempt. Once I declare this one a dead end, I shall turn my keyboard on its long end and sharpen it to a point. Then fall on it....<O>Edited by - Page47 on 04/15/2002 12:19:52 |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-04-15 : 12:50:43
|
| Take a look at this:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=14855How about:1. Use xp_cmdshell to run a "dir" command that will list the file2. Import the file(s) into a table3. Use IF EXISTS (SELECT * FROM FileNames WHERE Filename=@file) to see if the file is there4. If it is, use another xp_cmdshell to copy it to the other folder5. Construct a BULK INSERT statement dynamically, using the file name, and EXECUTE itI think this will work, and you won't need any COM calls at all, unless the file exists on a FTP site somewhere, in which case, you can use the FTP procedure here:http://www.sqlteam.com/item.asp?ItemID=6002Also, I think bcp and BULK INSERT use slightly different APIs now (in SQL2K), BULK INSERT is supposed to be faster. bcp can use a trusted connection with the -T flag (make sure it's a capital T) Look in Books Online, it has all of the flags for bcp. |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-04-15 : 13:06:04
|
I am doing (roughly) exactly as you describe. There are a couple wrinkles, like you can't start the bulk insert until the file has finished being copied . . . you get an OS error.quote: ...5. Construct a BULK INSERT statement dynamically, using the file name, and EXECUTE it...
The trouble lies here. I want to log actions. I don't wan the loging to rollback on transaction rollback. I can't use bulk insert and OLE Object creation in the same batch because I get the OLE DB 'STREAM' error. I can't use bcp because I need nt authentication . . . rock meets hard-place....<O> |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-04-15 : 13:49:58
|
ok, I am getting the same error from the BulkCopy object as from the ExecuteImmediate(@bulkinsertstatement), so I think my problem is in my formatfile?!? But peep dis ...exec master..xp_cmdshell 'bcp dataloadstaging..myFile in \\myServer\e$\newloads\myFile.txt /f\\myServer\e$\newloads\myFile.fmt /SmyServer /Usa /PXXXXX' ... runs like a champ, but dis ...bulk insert myFile from '\\myServer\e$\newloads\myFile.txt'with(formatfile = '\\myServer\e$\newloads\myFile.fmt') ... give me ...quote: Server: Msg 4863, Level 16, State 1, Line 1Bulk insert data conversion error (truncation) for row 1, column 56 (Col056).Server: Msg 7399, Level 16, State 1, Line 1OLE DB provider 'STREAM' reported an error. The provider did not give any information about the error.The statement has been terminated.
... I am running these two using the same file into the same table on the same server, each statement in its own batch in QA. The format file is HUGE with some 250 columns. Has anyone ever had trouble with a format file for bulk insert in sql server 7? I really didn't want to embed sql authentication credentials into these procs . . . (preparing to sharpent keyboard into a point) . . .<O> |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-04-15 : 14:24:54
|
quote: bcp can use a trusted connection with the -T flag (make sure it's a capital T) Look in Books Online, it has all of the flags for bcp.
Just so I know I'm not crazy...I did post that earlier, yes? OK, why do you need to copy the file to BULK INSERT it? Is it an FTP job? If not, I don't see why you'd need to copy it at all.Whenever I get an OLE DB stream error, it usually means there's a bogus character in the data file. I think BULK INSERT is more sensitive to this than bcp, because the APIs are different. If you can get bcp to work then you're OK. Incidentally, bcp occurs outside of the batch which calls it, so it won't roll back if the batch fails. You can still log the action using the SQL-DMO methods; you just don't need SQL-DMO to do the import.Edited by - robvolk on 04/15/2002 14:26:17 |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-04-15 : 14:43:05
|
| /T . . . Rob, so sorry, I totally missed that the first time and that simple little thing solve ALL of my problems . . . you are the Morpheus in the SQL Matrix . . . I'll take the blue pill . . . thank you.<O> |
 |
|
|
jhunt
Starting Member
21 Posts |
Posted - 2003-07-09 : 01:27:22
|
| Well this may not be the answer to the problem, but in response to the question of anyone having trouble bulk inserting itnot SQL server 7, I did. And it turned out to be a documented bugwhen there are column defaults set on numeric or decimal fields.http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b272292I worked around it by dropping the column default prior to the bulk insert and reinstatiing it afterwards. |
 |
|
|
|
|
|
|
|