Hallo All,
Please help in determining the best procedure for the following problem.
In a user interface, there is a provision to upload excel file. This is done by admin, once/twice in a day on a regular basis.
The code that i have used to upload excel file into sql server.
select *
into #temp FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\Files\29.09.10\working_290910.xls;HDR=YES;IMEX=1',
'SELECT * FROM [2XX$]')
Once the upload is successful, i am using the logic and deleting all the unnecessary records..finally i will get a set of records, which i move into the corresponding tables. Finally i DELETE the temp table that i used for uploading. Here #temp.
This procedure has many problems. As the structure of #temp is not always same as excel assumes the datatype of that particular field based on the analysis of the data it has in first n records. For a particular field sometimes it assumes, the datatype as nvarchar(255) and some times text. So few functions throw an error message on these particular fields on the execution of the stored procedure..
Last time i had a similar problem, LTRIM is not working on text field..As excel assumed the datatype for a field in table #temp as text..
So i modified the code in such a way by creating the table,
CREATE TABLE temp_invoice(
[Concession Number] varchar(30),
[Status] char(10),
[Sort] char(10),
[Task code text] varchar(60),
[Task resp#] varchar(10),
[Person who complete the task] varchar(50),
[Completed] datetime
)
so that i can fix the datatypes and then move the data from excel...
EDIT IN QUERY:-
insert into temp(Completed)
SELECT CASE Completed when '00.00.0000' THEN null else Completed END
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\Files\12.10.10\MC01_PCTA00012_20101012.xls;HDR=YES;IMEX=1', 'select * from [Tabelle1$]') AS A;
Now i am having error message based on data. If in the first few rows, the field completed has '00.00.000'. Then its working fine. If it does not have the value in first few rows, it throws an error message.
Now in my excel file the data for the field [Completed] is 00.00.0000, so the error message is "Arithmetic overflow error converting expression in the datetime data type. The statement has been terminated."
If i remove the records with date as 00.00.0000 then its working good. But i can't manuelly do it everytime. Now i need to find a work around this problem.
thanks a lot for your patience in reading such a long mail..I really have no clue, whether the procedure that i am using is correct or not. I am just changing the code to overcome the errors...
Please suggest me an appropriate procedure for this this kind of requirements.