|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Bulk Insert from ExcelI´m new to this subject. As a matter of a fact, the first time I´m trying
this. I need to run a BULK INSERT query where the datasource is an Excel file and the destination is a table in SQL 2000 Server. After looking at the Books on Line, I´ve tried first: BULK INSERT Database.dbo.Table_tbl FROM 'c:\files\File.xls' It returns an error "column 2 truncated". I need directions on this subject. Thanks -- Manuel Manuel,
BULK INSERT is only for text files. Usually, files with the extension .xls are not text files, but Excel format files that are in a more complicated format than text. In order to import an Excel file to SQL Server, use OPENQUERY or OPENROWSET. Something like this is what you want: insert into D.dbo.T select * from OpenRowSet( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\excel\Workbook.xls;HDR=YES;IMEX=1' ,'select * from [SheetName$]' ) There are a number of issues regarding type guessing, column names, and importing regions of a worksheet, some of which are answered in previous news threads: http://groups.google.com/groups/search?q=kass+sqlserver+excel+openrowset It might also be the case that you can obtain your Excel files in comma-separated value format (CSV). If you can, then you should be able to use BULK INSERT with a format file or appropriate choices of delimiters. -- Steve Kass -- Drew University -- http://www.stevekass.com -- C50C2FA7-D789-49F8-A919-ABE0A32D754D Manuel wrote: Show quote >I´m new to this subject. As a matter of a fact, the first time I´m trying >this. >I need to run a BULK INSERT query where the datasource is an Excel file and >the destination is a table in SQL 2000 Server. >After looking at the Books on Line, I´ve tried first: >BULK INSERT Database.dbo.Table_tbl > FROM 'c:\files\File.xls' >It returns an error "column 2 truncated". >I need directions on this subject. >Thanks > > > Steve,
Thank you so much for your help. I´ll be studying your references, trying your suggestion or trying the csv file. I´m sure I´ll get back to you with more questions. Thanks in advance. -- Show quoteManuel "Steve Kass" wrote: > Manuel, > > BULK INSERT is only for text files. Usually, files with the > extension .xls are not text files, but Excel format files that are > in a more complicated format than text. > > In order to import an Excel file to SQL Server, use OPENQUERY > or OPENROWSET. Something like this is what you want: > > insert into D.dbo.T > select * from OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\excel\Workbook.xls;HDR=YES;IMEX=1' > ,'select * from [SheetName$]' > ) > > There are a number of issues regarding type guessing, column names, > and importing regions of a worksheet, some of which are answered in > previous news threads: > > http://groups.google.com/groups/search?q=kass+sqlserver+excel+openrowset > > It might also be the case that you can obtain your Excel files in > comma-separated value format (CSV). If you can, then you should > be able to use BULK INSERT with a format file or appropriate > choices of delimiters. > > -- Steve Kass > -- Drew University > -- http://www.stevekass.com > -- C50C2FA7-D789-49F8-A919-ABE0A32D754D > > Manuel wrote: > > >I´m new to this subject. As a matter of a fact, the first time I´m trying > >this. > >I need to run a BULK INSERT query where the datasource is an Excel file and > >the destination is a table in SQL 2000 Server. > >After looking at the Books on Line, I´ve tried first: > >BULK INSERT Database.dbo.Table_tbl > > FROM 'c:\files\File.xls' > >It returns an error "column 2 truncated". > >I need directions on this subject. > >Thanks > > > > > > > Steve,
I´ve tried this: insert into iterartis.dbo.projectoItems_tbl select * from OpenRowSet( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' ,'select * from [Folha1$]' ) It returned this error: Insert Error: Column name or number of supplied values does not match table definition. How do I accomplish this match. Thanks again -- Show quoteManuel "Steve Kass" wrote: > Manuel, > > BULK INSERT is only for text files. Usually, files with the > extension .xls are not text files, but Excel format files that are > in a more complicated format than text. > > In order to import an Excel file to SQL Server, use OPENQUERY > or OPENROWSET. Something like this is what you want: > > insert into D.dbo.T > select * from OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\excel\Workbook.xls;HDR=YES;IMEX=1' > ,'select * from [SheetName$]' > ) > > There are a number of issues regarding type guessing, column names, > and importing regions of a worksheet, some of which are answered in > previous news threads: > > http://groups.google.com/groups/search?q=kass+sqlserver+excel+openrowset > > It might also be the case that you can obtain your Excel files in > comma-separated value format (CSV). If you can, then you should > be able to use BULK INSERT with a format file or appropriate > choices of delimiters. > > -- Steve Kass > -- Drew University > -- http://www.stevekass.com > -- C50C2FA7-D789-49F8-A919-ABE0A32D754D > > Manuel wrote: > > >I´m new to this subject. As a matter of a fact, the first time I´m trying > >this. > >I need to run a BULK INSERT query where the datasource is an Excel file and > >the destination is a table in SQL 2000 Server. > >After looking at the Books on Line, I´ve tried first: > >BULK INSERT Database.dbo.Table_tbl > > FROM 'c:\files\File.xls' > >It returns an error "column 2 truncated". > >I need directions on this subject. > >Thanks > > > > > > > There is apparently a mismatch between the columns of the source and
destination. Look at the column lists of the table and the Excel sheet: select top 1 * from iterartis.dbo.projectoItems_tbl select top 1 * from OpenRowSet( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' ,'select * from [Folha1$]' ) You can probably solve this problem by specifying the column names explicitly. For example, you might have to write: insert into iterartis.dbo.projectoItems_tbl(columnname1,columnname2, ...) select somecolumn, othercolumn, thirdcolumn, ... from OpenRowSet( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' ,'select * from [Folha1$]' ) SK Manuel wrote: Show quote >Steve, > >I´ve tried this: > >insert into iterartis.dbo.projectoItems_tbl >select * from OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > ,'select * from [Folha1$]' >) > >It returned this error: > >Insert Error: Column name or number of supplied values does not match table >definition. > >How do I accomplish this match. > >Thanks again > > > Hi Steve,
What I did, was to create a list in the Excel file, named the columns as the columns of the database table and follow your advice, specifying the column names in the query. It worked beautifully ;-) Learned one more today ;-) Thank you so much for your help. -- Show quoteManuel "Steve Kass" wrote: > There is apparently a mismatch between the columns of the source and > destination. > > Look at the column lists of the table and the Excel sheet: > > select top 1 * from iterartis.dbo.projectoItems_tbl > > select top 1 * from > > OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > ,'select * from [Folha1$]' > ) > > You can probably solve this problem by specifying the column names > explicitly. For example, you might have to write: > > insert into iterartis.dbo.projectoItems_tbl(columnname1,columnname2, ...) > select somecolumn, othercolumn, thirdcolumn, ... > from > OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > ,'select * from [Folha1$]' > ) > > SK > > > Manuel wrote: > > >Steve, > > > >I´ve tried this: > > > >insert into iterartis.dbo.projectoItems_tbl > >select * from OpenRowSet( > > 'Microsoft.Jet.OLEDB.4.0', > > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > > ,'select * from [Folha1$]' > >) > > > >It returned this error: > > > >Insert Error: Column name or number of supplied values does not match table > >definition. > > > >How do I accomplish this match. > > > >Thanks again > > > > > > > Steve,
As I said in the previous post, you've anwered my question and the import works beautifully. But I've another question. Suppose that the first column is named ProjectID. I want to run a query before the insert to prevent duplication. I´m using Dreamweaver 8.0 and ColdFusion. Usually I run a query Like; <cfquery name="q1" datasource="DatabaseName"> SELECT ProjectoID FROM dbo.ProjectoItems_tbl WHERE ProjectoID = '#Form.ProjectoID#' </cfquery> <cfif q1.recordcount GT 0> <cfoutput>message alerting the record already exists</cfoutput> </cfif> How do I refer to ProjectID in the Excel spreadsheet to substitute in query q1 '#Form.ProjectoID#'? Thanks for your time and your help. -- Show quoteManuel "Steve Kass" wrote: > There is apparently a mismatch between the columns of the source and > destination. > > Look at the column lists of the table and the Excel sheet: > > select top 1 * from iterartis.dbo.projectoItems_tbl > > select top 1 * from > > OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > ,'select * from [Folha1$]' > ) > > You can probably solve this problem by specifying the column names > explicitly. For example, you might have to write: > > insert into iterartis.dbo.projectoItems_tbl(columnname1,columnname2, ...) > select somecolumn, othercolumn, thirdcolumn, ... > from > OpenRowSet( > 'Microsoft.Jet.OLEDB.4.0', > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > ,'select * from [Folha1$]' > ) > > SK > > > Manuel wrote: > > >Steve, > > > >I´ve tried this: > > > >insert into iterartis.dbo.projectoItems_tbl > >select * from OpenRowSet( > > 'Microsoft.Jet.OLEDB.4.0', > > 'Excel 8.0;Database=c:\files\_ImportSQL\testImport.xls;HDR=YES;IMEX=1' > > ,'select * from [Folha1$]' > >) > > > >It returned this error: > > > >Insert Error: Column name or number of supplied values does not match table > >definition. > > > >How do I accomplish this match. > > > >Thanks again > > > > > > > If the Excel spreadsheet only has one row, and you are correctly
seeing ProjectoID as the column name with the OPENROWSET statement, it could be like this: SELECT ProjectoID FROM dbo.ProjectoItems_tbl WHERE ProjectoID = ( SELECT ProjectoID FROM OPENROWSET(... <same as before> ) ) If there is more than one row in the Excel "table", you will have to identify it further with a WHERE clause based on other columns: SELECT ProjectoID FROM dbo.ProjectoItems_tbl WHERE ProjectoID = ( SELECT ProjectoID FROM OPENROWSET(... <same as before> ) WHERE ... ) SK Manuel wrote: Show quote >Steve, > >As I said in the previous post, you've anwered my question and the import >works beautifully. But I've another question. >Suppose that the first column is named ProjectID. >I want to run a query before the insert to prevent duplication. I´m using >Dreamweaver 8.0 and ColdFusion. Usually I run a query Like; > ><cfquery name="q1" datasource="DatabaseName"> > SELECT ProjectoID > FROM dbo.ProjectoItems_tbl > WHERE ProjectoID = '#Form.ProjectoID#' ></cfquery> ><cfif q1.recordcount GT 0> ><cfoutput>message alerting the record already exists</cfoutput> ></cfif> > >How do I refer to ProjectID in the Excel spreadsheet to substitute in query >q1 '#Form.ProjectoID#'? > >Thanks for your time and your help. > > Hi Steve,
Thanks for your reply. I´m still missing something. Please be patient as I´m a 58 year old guy that decided to learn some programming just a year ago. I really do have more than one row in the excel Spreadsheet. Suppose I have 3 columns. 'ProjectoID', 'Column 2' and 'Column3' named the same in the database table. The Spreadsheet will have many rows (number unknown). How do I refer to them in the second WHERE clause? Would it just be: WHERE Column2 = 'Column2' AND Column3 = 'Column3' What I need to accomplish is to prevent the insertion of the Spreadsheet if someone hits F5 or reloads the browser or the Spreadsheet has not been modified. Thanks again. -- Show quoteManuel "Steve Kass" wrote: > If the Excel spreadsheet only has one row, and you are correctly > seeing ProjectoID as the column name with the OPENROWSET > statement, it could be like this: > > SELECT ProjectoID > FROM dbo.ProjectoItems_tbl > WHERE ProjectoID = ( > SELECT ProjectoID > FROM OPENROWSET(... <same as before> ) > ) > > If there is more than one row in the Excel "table", you will > have to identify it further with a WHERE clause based on > other columns: > > SELECT ProjectoID > FROM dbo.ProjectoItems_tbl > WHERE ProjectoID = ( > SELECT ProjectoID > FROM OPENROWSET(... <same as before> ) > WHERE ... > ) > > SK > > Manuel wrote: > > >Steve, > > > >As I said in the previous post, you've anwered my question and the import > >works beautifully. But I've another question. > >Suppose that the first column is named ProjectID. > >I want to run a query before the insert to prevent duplication. I´m using > >Dreamweaver 8.0 and ColdFusion. Usually I run a query Like; > > > ><cfquery name="q1" datasource="DatabaseName"> > > SELECT ProjectoID > > FROM dbo.ProjectoItems_tbl > > WHERE ProjectoID = '#Form.ProjectoID#' > ></cfquery> > ><cfif q1.recordcount GT 0> > ><cfoutput>message alerting the record already exists</cfoutput> > ></cfif> > > > >How do I refer to ProjectID in the Excel spreadsheet to substitute in query > >q1 '#Form.ProjectoID#'? > > > >Thanks for your time and your help. > > > > > Manuel,
You can join the Excel "table" like any other database table SELECT T.ProjectoID FROM dbo.ProjectoItems_tbl AS T JOIN ( SELECT ProjectoID FROM OPENROWSET(... <same as before> ) ) AS E ON T.ProjectoID = E.ProjectoID or for example, to insert rows that are not already in the table: INSERT INTO dbo.ProjectoItems_tbl FROM ( SELECT ProjectoID FROM OPENROWSET(... <same as before> ) ) AS E WHERE NOT EXISTS ( SELECT * FROM dbo.ProjectItems_tbl AS T WHERE T.ProjectID = E.ProjectoID ) Probably for what you need here, there is nothing you need to do differently because you are working with Excel, other than refer to the Excel data using OPENROWSET. SK Manuel wrote: Show quote >Hi Steve, >Thanks for your reply. >I´m still missing something. Please be patient as I´m a 58 year old guy that >decided to learn some programming just a year ago. >I really do have more than one row in the excel Spreadsheet. >Suppose I have 3 columns. 'ProjectoID', 'Column 2' and 'Column3' named the >same in the database table. The Spreadsheet will have many rows (number >unknown). >How do I refer to them in the second WHERE clause? Would it just be: >WHERE Column2 = 'Column2' AND Column3 = 'Column3' > >What I need to accomplish is to prevent the insertion of the Spreadsheet if >someone hits F5 or reloads the browser or the Spreadsheet has not been >modified. >Thanks again. > > Thanks once again Steve.
I'm afraid I haven´t made the scenario clear enough. I´ll try and to make things easier "projecto" in portuguese becomes "project". I have a main table called "project_tbl" with PK "projectID". I have a secondary table called "projectItems_tbl". "project_tbl.projectID" = "projectItems_tbl.projectID". Then I have the Excel Spreadsheet where the first column is called "projectID", followed by other columns where the description or parameters of Items are described like "Length", "Width", "Height". So, I can have in this Spreadsheet, many rows with the same "projectID", as all those items belong to the same "project". Actually what I thought, was just to compare all the columns on the first row of the spreadsheet and find out if that instance was already in "projectItems_tbl". That would mean that either the page was reloaded, or inadvertently someone try to insert what was already inserted. As a procedure, the insertion is only to be executed after a new spreadsheet is created for items of the next "projectID" overwriting the previous one, so it keeps the same name. I hope my portuguese-english makes sense to you. Thanks so much. Manuel Show quote "Steve Kass" wrote: > Manuel, > > You can join the Excel "table" like any other database table > > SELECT T.ProjectoID > FROM dbo.ProjectoItems_tbl AS T > JOIN ( > SELECT ProjectoID > FROM OPENROWSET(... <same as before> ) > ) AS E > ON T.ProjectoID = E.ProjectoID > > or for example, to insert rows that are not already in the table: > > INSERT INTO dbo.ProjectoItems_tbl > FROM ( > SELECT ProjectoID > FROM OPENROWSET(... <same as before> ) > ) AS E > WHERE NOT EXISTS ( > SELECT * FROM dbo.ProjectItems_tbl AS T > WHERE T.ProjectID = E.ProjectoID > ) > > Probably for what you need here, there is nothing you need > to do differently because you are working with Excel, other > than refer to the Excel data using OPENROWSET. > > SK > > Manuel wrote: > > >Hi Steve, > >Thanks for your reply. > >I´m still missing something. Please be patient as I´m a 58 year old guy that > >decided to learn some programming just a year ago. > >I really do have more than one row in the excel Spreadsheet. > >Suppose I have 3 columns. 'ProjectoID', 'Column 2' and 'Column3' named the > >same in the database table. The Spreadsheet will have many rows (number > >unknown). > >How do I refer to them in the second WHERE clause? Would it just be: > >WHERE Column2 = 'Column2' AND Column3 = 'Column3' > > > >What I need to accomplish is to prevent the insertion of the Spreadsheet if > >someone hits F5 or reloads the browser or the Spreadsheet has not been > >modified. > >Thanks again. > > > > > Manuel,
I'm not sure I understand. If you only need to compare data (values in the columns) between Excel and your database table, there is nothing special about using Excel. Just use the OPENROWSET function like a table. If you need to look at the column headers in Excel to see if they match the columns of some particular table, then you could change HDR=YES to HDR=NO and use SELECT TOP 1. While TOP 1 is not guaranteed to give the first row (which with HDR=NO is the column names), I think it is relatively safe to assume it will. (I'm not sure this is what you want, but it is the only thing I can think of where you would do something differently with Excel than with a table. If I'm still misunderstanding you, can you be more specific about what you have and what you need? For example: I have a spreadsheet with the following structure: ProjectID Length Width Height 4 5 6 8 2 3 9 8 .... and I have the following tables: ProjectTable, which looks like this: (show the column names and a few rows) ProjectItemsTable, which looks like this: (show the column names and a few rows) I want to [find out| insert| ...] ..., which for the data shown above, will have the result... It's often possible to import the Excel data into a table and then use the table to answer your questions, which might be easier: select * into #temporary_holding_table from OPENROWSET(... Steve Manuel wrote: Show quote >Thanks once again Steve. >I'm afraid I haven´t made the scenario clear enough. I´ll try and to make >things easier "projecto" in portuguese becomes "project". >I have a main table called "project_tbl" with PK "projectID". I have a >secondary table called "projectItems_tbl". "project_tbl.projectID" = >"projectItems_tbl.projectID". >Then I have the Excel Spreadsheet where the first column is called >"projectID", followed by other columns where the description or parameters of >Items are described like "Length", "Width", "Height". >So, I can have in this Spreadsheet, many rows with the same "projectID", as >all those items belong to the same "project". >Actually what I thought, was just to compare all the columns on the first >row of the spreadsheet and find out if that instance was already in >"projectItems_tbl". That would mean that either the page was reloaded, or >inadvertently someone try to insert what was already inserted. As a >procedure, the insertion is only to be executed after a new spreadsheet is >created for items of the next "projectID" overwriting the previous one, so it >keeps the same name. >I hope my portuguese-english makes sense to you. >Thanks so much. > >Manuel > > >"Steve Kass" wrote: > > > >>Manuel, >> >>You can join the Excel "table" like any other database table >> >>SELECT T.ProjectoID >>FROM dbo.ProjectoItems_tbl AS T >>JOIN ( >> SELECT ProjectoID >> FROM OPENROWSET(... <same as before> ) >>) AS E >>ON T.ProjectoID = E.ProjectoID >> >>or for example, to insert rows that are not already in the table: >> >>INSERT INTO dbo.ProjectoItems_tbl >>FROM ( >> SELECT ProjectoID >> FROM OPENROWSET(... <same as before> ) >>) AS E >>WHERE NOT EXISTS ( >> SELECT * FROM dbo.ProjectItems_tbl AS T >> WHERE T.ProjectID = E.ProjectoID >>) >> >>Probably for what you need here, there is nothing you need >>to do differently because you are working with Excel, other >>than refer to the Excel data using OPENROWSET. >> >>SK >> >>Manuel wrote: >> >> >> >>>Hi Steve, >>>Thanks for your reply. >>>I´m still missing something. Please be patient as I´m a 58 year old guy that >>>decided to learn some programming just a year ago. >>>I really do have more than one row in the excel Spreadsheet. >>>Suppose I have 3 columns. 'ProjectoID', 'Column 2' and 'Column3' named the >>>same in the database table. The Spreadsheet will have many rows (number >>>unknown). >>>How do I refer to them in the second WHERE clause? Would it just be: >>>WHERE Column2 = 'Column2' AND Column3 = 'Column3' >>> >>>What I need to accomplish is to prevent the insertion of the Spreadsheet if >>>someone hits F5 or reloads the browser or the Spreadsheet has not been >>>modified. >>>Thanks again. >>> >>> >>> >>> Let me try to be more specific and at the same time it might help explain a
bit of what is behind all this. This is a company (my daughter´s) that moves objects of art (the items) within Europe. The project is the job of moving for example a collection of art from Museum A in Madrid to Museum B in Lisbon. project_tbl (some colums) projectID(autoNumber) projectCityOfOrigin projectCityOfDestination 1 Madrid Lisbon 2 Paris Madrid Then I have projectItems_tbl projectItemID(autoNumber) projectID Length Height Width 1 1 2.5 3.5 6 2 1 3.2 4.5 7 3 1 3.5 4.2 8 The customers have a supplied Excel template with the exact same column names as projectItems_tbl, without obviously "projectoItemID", and "projectID" that they leave blank as they don´t know what will it be, that they fill with the description of the items to be moved and send it by E-mail. Once received, the new job is created. Knowing the new "projectID", whoever receives the Spreadsheet fills all rows with that specific "projectID" and saves it always with the same name, the one that the query refers to. Ideally would be to detect any row in the Spreadsheet with the same values as in "projectItems_tbl". For example 1, 3.5, 4.2, 8. That item, wich is "projectItemID=3" already exists belonging to projectID=1 and with the same values. I really hope you undestand what I´m trying to achieve. Thank you very much -- Show quoteManuel "Steve Kass" wrote: > Manuel, > > I'm not sure I understand. If you only need to compare data (values in > the columns) > between Excel and your database table, there is nothing special about > using Excel. > Just use the OPENROWSET function like a table. > > If you need to look at the column headers in Excel to see if they match the > columns of some particular table, then you could change HDR=YES to > HDR=NO and use SELECT TOP 1. While TOP 1 is not guaranteed > to give the first row (which with HDR=NO is the column names), I > think it is relatively safe to assume it will. (I'm not sure this is what > you want, but it is the only thing I can think of where you would > do something differently with Excel than with a table. > > If I'm still misunderstanding you, can you be more specific about > what you have and what you need? For example: > > I have a spreadsheet with the following structure: > > ProjectID Length Width Height > 4 5 6 8 > 2 3 9 8 > .... > > and I have the following tables: > > ProjectTable, which looks like this: > (show the column names and a few rows) > > ProjectItemsTable, which looks like this: > (show the column names and a few rows) > > I want to [find out| insert| ...] ..., which for the data > shown above, will have the result... > > It's often possible to import the Excel data into > a table and then use the table to answer your questions, > which might be easier: > > select * into #temporary_holding_table > from OPENROWSET(... > > Steve > > Manuel wrote: > > >Thanks once again Steve. > >I'm afraid I haven´t made the scenario clear enough. I´ll try and to make > >things easier "projecto" in portuguese becomes "project". > >I have a main table called "project_tbl" with PK "projectID". I have a > >secondary table called "projectItems_tbl". "project_tbl.projectID" = > >"projectItems_tbl.projectID". > >Then I have the Excel Spreadsheet where the first column is called > >"projectID", followed by other columns where the description or parameters of > >Items are described like "Length", "Width", "Height". > >So, I can have in this Spreadsheet, many rows with the same "projectID", as > >all those items belong to the same "project". > >Actually what I thought, was just to compare all the columns on the first > >row of the spreadsheet and find out if that instance was already in > >"projectItems_tbl". That would mean that either the page was reloaded, or > >inadvertently someone try to insert what was already inserted. As a > >procedure, the insertion is only to be executed after a new spreadsheet is > >created for items of the next "projectID" overwriting the previous one, so it > >keeps the same name. > >I hope my portuguese-english makes sense to you. > >Thanks so much. > > > >Manuel > > > > > >"Steve Kass" wrote: > > > > > > > >>Manuel, > >> > >>You can join the Excel "table" like any other database table > >> > >>SELECT T.ProjectoID > >>FROM dbo.ProjectoItems_tbl AS T > >>JOIN ( > >> SELECT ProjectoID > >> FROM OPENROWSET(... <same as before> ) > >>) AS E > >>ON T.ProjectoID = E.ProjectoID > >> > >>or for example, to insert rows that are not already in the table: > >> > >>INSERT INTO dbo.ProjectoItems_tbl > >>FROM ( > >> SELECT ProjectoID > >> FROM OPENROWSET(... <same as before> ) > >>) AS E > >>WHERE NOT EXISTS ( > >> SELECT * FROM dbo.ProjectItems_tbl AS T > >> WHERE T.ProjectID = E.ProjectoID > >>) > >> > >>Probably for what you need here, there is nothing you need > >>to do differently because you are working with Excel, other > >>than refer to the Excel data using OPENROWSET. > >> > >>SK > >> > >>Manuel wrote: > >> > >> > >> > >>>Hi Steve, > >>>Thanks for your reply. > >>>I´m still missing something. Please be patient as I´m a 58 year old guy that > >>>decided to learn some programming just a year ago. > >>>I really do have more than one row in the excel Spreadsheet. > >>>Suppose I have 3 columns. 'ProjectoID', 'Column 2' and 'Column3' named the > >>>same in the database table. The Spreadsheet will have many rows (number > >>>unknown). > >>>How do I refer to them in the second WHERE clause? Would it just be: > >>>WHERE Column2 = 'Column2' AND Column3 = 'Column3' > >>> > >>>What I need to accomplish is to prevent the insertion of the Spreadsheet if > >>>someone hits F5 or reloads the browser or the Spreadsheet has not been > >>>modified. > >>>Thanks again. > >>> > >>> > >>> > >>> > Manuel,
It's not really an Excel issue at this point, but I think what you want is SELECT * FROM OPENROWSET(... <same as before> ) ) AS E WHERE EXISTS ( SELECT * FROM dbo.ProjectItems_tbl AS T WHERE T.Length= E.Length AND T.Height = E.Height AND T.Width = E.Width ) In other words, find the items in the spreadsheet that look like they are already in the table. (As written, for any projectID value, but if you want to check if they are in the table only for a specific projectID value (3 for this example): SELECT * FROM OPENROWSET(... <same as before> ) ) AS E WHERE EXISTS ( SELECT * FROM dbo.ProjectItems_tbl AS T WHERE T.Length= E.Length AND T.Height = E.Height AND T.Width = E.Width ) AND ProjectID = 3 I left out checking for a match on ItemID also, since it is an auto number, and could be different in the spreadsheet from the number it is in the table, but if that discripancy can't happen, then you would include the additional condition in the where clause. SK Manuel wrote: Show quote >Let me try to be more specific and at the same time it might help explain a >bit of what is behind all this. >This is a company (my daughter´s) that moves objects of art (the items) >within Europe. The project is the job of moving for example a collection of >art from Museum A in Madrid to Museum B in Lisbon. > >project_tbl (some colums) > >projectID(autoNumber) projectCityOfOrigin projectCityOfDestination > >1 Madrid Lisbon >2 Paris Madrid > >Then I have projectItems_tbl > >projectItemID(autoNumber) projectID Length Height Width > >1 1 2.5 >3.5 6 >2 1 3.2 >4.5 7 >3 1 3.5 >4.2 8 > >The customers have a supplied Excel template with the exact same column >names as projectItems_tbl, without obviously "projectoItemID", and >"projectID" that they leave blank as they don´t know what will it be, that >they fill with the description of the items to be moved and send it by E-mail. >Once received, the new job is created. Knowing the new "projectID", whoever >receives the Spreadsheet fills all rows with that specific "projectID" and >saves it always with the same name, the one that the query refers to. >Ideally would be to detect any row in the Spreadsheet with the same values >as in "projectItems_tbl". For example 1, 3.5, 4.2, 8. >That item, wich is "projectItemID=3" already exists belonging to projectID=1 >and with the same values. > >I really hope you undestand what I´m trying to achieve. > >Thank you very much > > > > > That's exactly it. The first part (for any ProjectID).
And I understand it ;-) Thanks for everything. -- Show quoteManuel "Steve Kass" wrote: > Manuel, > > It's not really an Excel issue at this point, but I think what you want is > > SELECT * > FROM OPENROWSET(... <same as before> ) > ) AS E > WHERE EXISTS ( > SELECT * FROM dbo.ProjectItems_tbl AS T > WHERE T.Length= E.Length > AND T.Height = E.Height > AND T.Width = E.Width > ) > > In other words, find the items in the spreadsheet that look like > they are already in the table. (As written, for any projectID > value, but if you want to check if they are in the table only > for a specific projectID value (3 for this example): > > SELECT * > FROM OPENROWSET(... <same as before> ) > ) AS E > WHERE EXISTS ( > SELECT * FROM dbo.ProjectItems_tbl AS T > WHERE T.Length= E.Length > AND T.Height = E.Height > AND T.Width = E.Width > ) > AND ProjectID = 3 > > > I left out checking for a match on ItemID also, since it is > an auto number, and could be different in the spreadsheet > from the number it is in the table, but if that discripancy can't > happen, then you would include the additional condition in > the where clause. > > SK > > Manuel wrote: > > >Let me try to be more specific and at the same time it might help explain a > >bit of what is behind all this. > >This is a company (my daughter´s) that moves objects of art (the items) > >within Europe. The project is the job of moving for example a collection of > >art from Museum A in Madrid to Museum B in Lisbon. > > > >project_tbl (some colums) > > > >projectID(autoNumber) projectCityOfOrigin projectCityOfDestination > > > >1 Madrid Lisbon > >2 Paris Madrid > > > >Then I have projectItems_tbl > > > >projectItemID(autoNumber) projectID Length Height Width > > > >1 1 2.5 > >3.5 6 > >2 1 3.2 > >4.5 7 > >3 1 3.5 > >4.2 8 > > > >The customers have a supplied Excel template with the exact same column > >names as projectItems_tbl, without obviously "projectoItemID", and > >"projectID" that they leave blank as they don´t know what will it be, that > >they fill with the description of the items to be moved and send it by E-mail. > >Once received, the new job is created. Knowing the new "projectID", whoever > >receives the Spreadsheet fills all rows with that specific "projectID" and > >saves it always with the same name, the one that the query refers to. > >Ideally would be to detect any row in the Spreadsheet with the same values > >as in "projectItems_tbl". For example 1, 3.5, 4.2, 8. > >That item, wich is "projectItemID=3" already exists belonging to projectID=1 > >and with the same values. > > > >I really hope you undestand what I´m trying to achieve. > > > >Thank you very much > > > > > > > > > > > Manuel wrote:
Show quote > I´m new to this subject. As a matter of a fact, the first time I´m trying You may need to specify a format file to resolve this. What kind of> this. > I need to run a BULK INSERT query where the datasource is an Excel file and > the destination is a table in SQL 2000 Server. > After looking at the Books on Line, I´ve tried first: > BULK INSERT Database.dbo.Table_tbl > FROM 'c:\files\File.xls' > It returns an error "column 2 truncated". > I need directions on this subject. > Thanks > > -- > Manuel data are we looking at Another alternative.
http://www.sqlservercentral.com/columnists/sholliday/leveragingxpexcelxmlandopenxmlfordataimports.asp Show quote "Manuel" <Man***@discussions.microsoft.com> wrote in message news:8C7D716F-05A9-4FC0-9C0C-43F49FE7EFBE@microsoft.com... > I´m new to this subject. As a matter of a fact, the first time I´m trying > this. > I need to run a BULK INSERT query where the datasource is an Excel file and > the destination is a table in SQL 2000 Server. > After looking at the Books on Line, I´ve tried first: > BULK INSERT Database.dbo.Table_tbl > FROM 'c:\files\File.xls' > It returns an error "column 2 truncated". > I need directions on this subject. > Thanks > > -- > Manuel |
|||||||||||||||||||||||