benbotektips
MIS
I have some files on the AS400 to be copied into SQL-Server, for further massage/ETL. I'm copying an existing DTS package, and everything works except adding fields onto an existing table. I think I'm misunderstanding the 'INSERT...SELECT' functionality.
In the AS400 files, there's a date field 'sdate' defined as decimal, precision 6, scale 0 - meaning June 20 2005, would be '62005'. I need to add a field with the reformatted date '06/20/2005'. The existing DTS package I'm copying from has the SQL something like this (exact SQL unavailable, server is down), where 'saledate' is the newly formatted field:
insert SALES
select *,
saledate = (case when sdate = 0 then 0 else
cast
(stuff(stuff(sdate + 1000000, 7) 3, 0, '/',) 6,0,'/')
as smalldatetime
)
from AS400a.saleslib.salesdata.allsales
The sytax is probably off a little, but even if so, the point is, it looks like the SQL is doing 3 things at once; selecting all fields from the original file to copy to the table, defining a new field for the table, and filling it with the data as formatted.
When I try to copy this for another table, I get an error message back telling me there's no such column, or column not found. This leads me to think the 'INSERT...SELECT' can't both define a new column (like 'ALTER TABLE' does), AND fill it with data. Yet, in the tables from the original DTS package, I don't see any other place where new columns are defined, like with 'ALTER TABLE'. In the AS400 library & the SQL-server staging database, there's only the original date field, 'sdate'; in the summary (final) database, SQL-Server table 'SALES' has the field 'saledate', in the format 'mm/dd/yyyy', with the '/'.
So, can the 'INSERT...SELECT' statement select all fields from an existing AS400 file, define a new column, AND fill it up with reformatted data,
OR,
is it necessary to 1st create the table in SQL-Server with all the original fields, ALTER TABLE to add the new column 'saledate smalldatetime', (which must have been done manually when the original table was 1st created on SQL-Server), then run the 'INSERT...SELECT'?
In the AS400 files, there's a date field 'sdate' defined as decimal, precision 6, scale 0 - meaning June 20 2005, would be '62005'. I need to add a field with the reformatted date '06/20/2005'. The existing DTS package I'm copying from has the SQL something like this (exact SQL unavailable, server is down), where 'saledate' is the newly formatted field:
insert SALES
select *,
saledate = (case when sdate = 0 then 0 else
cast
(stuff(stuff(sdate + 1000000, 7) 3, 0, '/',) 6,0,'/')
as smalldatetime
)
from AS400a.saleslib.salesdata.allsales
The sytax is probably off a little, but even if so, the point is, it looks like the SQL is doing 3 things at once; selecting all fields from the original file to copy to the table, defining a new field for the table, and filling it with the data as formatted.
When I try to copy this for another table, I get an error message back telling me there's no such column, or column not found. This leads me to think the 'INSERT...SELECT' can't both define a new column (like 'ALTER TABLE' does), AND fill it with data. Yet, in the tables from the original DTS package, I don't see any other place where new columns are defined, like with 'ALTER TABLE'. In the AS400 library & the SQL-server staging database, there's only the original date field, 'sdate'; in the summary (final) database, SQL-Server table 'SALES' has the field 'saledate', in the format 'mm/dd/yyyy', with the '/'.
So, can the 'INSERT...SELECT' statement select all fields from an existing AS400 file, define a new column, AND fill it up with reformatted data,
OR,
is it necessary to 1st create the table in SQL-Server with all the original fields, ALTER TABLE to add the new column 'saledate smalldatetime', (which must have been done manually when the original table was 1st created on SQL-Server), then run the 'INSERT...SELECT'?