|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, July 15, 2010 9:09 AM
Points: 3,
Visits: 4
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, March 04, 2013 9:42 AM
Points: 107,
Visits: 34
|
|
Getting error: Msg 197, Level 15, State 1, Line 19 EXECUTE cannot be used as a source when inserting into a table variable. Msg 170, Level 15, State 1, Line 27 Line 27: Incorrect syntax near 'try'. Msg 170, Level 15, State 1, Line 36 Line 36: Incorrect syntax near 'try'. Msg 156, Level 15, State 1, Line 40 Incorrect syntax near the keyword 'END'.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 6:45 AM
Points: 1,441,
Visits: 2,485
|
|
On the first attempt I received the message:
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.
I turned that feature on & it worked fine.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, April 18, 2013 8:47 PM
Points: 2,073,
Visits: 525
|
|
This is a very useful script. However, there is really no need to use a cursor here. The code can be simplified just by deleting rows which do not contain a date within the first 10 characters and then processing the remaining set.
DECLARE @FilePath varchar(200);
SET @FilePath = 'dir C:\temp\*.pdf /-C '
--EXEC master.dbo.xp_cmdshell 'dir /?' --EXEC master.dbo.xp_cmdshell 'dir c:\*.txt /-C '
DECLARE @Files TABLE (FileName varchar(200), FileSize varchar (20), DateCreated datetime); DECLARE @Dir TABLE (FileInfo varchar(1000));
INSERT INTO @Dir EXEC master.dbo.xp_cmdshell @FilePath;
DELETE FROM @Dir WHERE ISDATE(SUBSTRING(FileInfo, 1, 21)) = 0;
INSERT INTO @Files (DateCreated, FileSize, FileName) SELECT DateCreated = CONVERT(datetime, SUBSTRING(FileInfo, 1, 21)), FileSize = SUBSTRING(LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))), 0, CHARINDEX(' ', LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))))), FileName = SUBSTRING(LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))), CHARINDEX(' ', LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo)))), LEN(LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))))) FROM @Dir;
SELECT * FROM @Files;
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 6:14 AM
Points: 137,
Visits: 592
|
|
I would not use a CURSOR here, well anywhere really.... What I would do if you needed to iterate through a logic then I would use a TABLE VARIABLE.
There is however no need for iteration as stated here.
Good little script however.
<hr noshade size='1' width='250' color='#BBC8E5'>
Regards,
Jeffery Williams http://www.linkedin.com/in/jwilliamsoh
|
|
|
|