Question

I am in a middle of migrating data from excel to a database in SQL by importing it, thus making physical tables with fields nvarchar(255) as its datatype, the elements inside the excel file came from the same copy of the database where it would be imported but from a different environment so i am sure that the data inside the excel file are compatible in the fields where it would be imported. my struggle is as i import the data in excel to SQL it creates another Physical table with datatype nvarchar(255) while the real table has different datatypes for each fields

if im going to migrate all data from importedDataTable to real MyTable i would use something like the following code:

Insert into Mytable
(field1
,field2
..
..
..)
Select 
field1
,field2
..
..
..
from ImportedDatatable

by this method it would generate an error like

String or binary data would be truncated.

So in order to fix this i need to cast every single data from ImportedDatatable from selecting it in order for it to work, the problem is i do have many tables some of it consists of 100 fields and converting them one by one would take time.. so is there any other way to do this??

BTW. Linked server selection is not an option because the two servers came from different environment and the Company policy would not allow it to be linked. so any help?

Was it helpful?

Solution

There is no easy solution FOR this, I'm affraid. The two solutions I can think of are:

1) SSIS package: create OLE DB source and destination, and make it ignore truncation errors.

2) A stored procedure to Alter each nvarchar(255) columns to its corresponding column length on the destination table (taken from information_schema.columns)

In both cases, the fact that the column names match, makes the task not too daunting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top