Connecting Tech Pros Worldwide Help | Site Map

Use Excel modified date in Stored Procedure

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#1: 1 Week Ago
Within a stored procedure I am creating a table from an Excel spreadsheet
Expand|Select|Wrap|Line Numbers
  1. /*Create tmp table from XLS*/
  2. SELECT * INTO tmp_cons FROM
  3. OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;
  4. Database=\\[server]\path\to\Excel\spreadsheet.xls',
  5. 'SELECT * FROM [sheet1$]')
Then joining this table to update another with new products
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO consum_stk (part,qty)
  2. SELECT tmp_cons.Part,Stock FROM tmp_cons
  3.     LEFT JOIN consum_stk 
  4.     ON tmp_cons.Part = consum_stk.part
  5. WHERE consum_stk.part IS NULL
consum_stk also has a last_updated column with GETDATE() as default.
Is it possible to use the Excel file 'modified' date property to populate this field
best answer - posted by ck9663
Yes. I'm sure there are a number of other ways of doing it. Until someone suggest something else try the following pseudo-code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @tblDirCheck table (RowNum int IDENTITY(1,1),CmdResult varchar(max))
  3.  
  4. set @tsql = 'dir \\[server]\path\to\Excel\spreadsheet.xls /TW'
  5.  
  6. insert into @tblDirCheck(cmdresult)
  7.   exec @ErrorNumber = master..xp_cmdshell @tsql         
  8.  
  9.  
Now you'll have a single row on your table variable. Parse the cmdresult column to get your modified date/time. Just be careful, if one of the sheets was modified, the date/time will change even if your target sheet were not modified at all.

Happy Coding!!!


--- CK
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: 1 Week Ago

re: Use Excel modified date in Stored Procedure


Yes. A default value will be used if you did not specify any value into a column when the record was created. You just have to get the property of file from the OS side to get the value of the modified date.

Good luck!!!

--- CK
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#3: 1 Week Ago

re: Use Excel modified date in Stored Procedure


Thanks for replying
Quote:
You just have to get the property of file from the OS side to get the value of the modified date.
This is the bit I am stuck on.
Can this be done from a stored procedure?
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#4: 1 Week Ago

re: Use Excel modified date in Stored Procedure


Yes. I'm sure there are a number of other ways of doing it. Until someone suggest something else try the following pseudo-code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @tblDirCheck table (RowNum int IDENTITY(1,1),CmdResult varchar(max))
  3.  
  4. set @tsql = 'dir \\[server]\path\to\Excel\spreadsheet.xls /TW'
  5.  
  6. insert into @tblDirCheck(cmdresult)
  7.   exec @ErrorNumber = master..xp_cmdshell @tsql         
  8.  
  9.  
Now you'll have a single row on your table variable. Parse the cmdresult column to get your modified date/time. Just be careful, if one of the sheets was modified, the date/time will change even if your target sheet were not modified at all.

Happy Coding!!!


--- CK
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#5: 1 Week Ago

re: Use Excel modified date in Stored Procedure


Thanks ck9663. I will study your solution before implementing.
Particuarly the master..xp_cmdshell
Reply


Similar Microsoft SQL Server bytes