| 
                
                    | 
                            
                                | Author | Topic |  
                                    | MenorelStarting Member
 
 
                                        15 Posts | 
                                            
                                            |  Posted - 2013-06-28 : 07:54:26 
 |  
                                            |  I am importing multiple CSV files into a SQL Server 2005 table and I believe I am running into an issue with the data. Below is the way I have established the table, a sample of the data, and the stored procedure I am using to do the import.The error message that I am getting when executing the SP is:Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ','.Table ConfigurationTIMERECORDID         nvarchar(50)   CheckedExternalID       nvarchar(50)   CheckedFirstName        nvarchar(50)   CheckedLastName         nvarchar(50)   CheckedLastUpdatedDate      datetime   CheckedLastUpdatedTime      nvarchar(50)   CheckedLastUpdatedBy        nvarchar(50)   CheckedIsApproved       nvarchar(50)   CheckedEmployeeID       nvarchar(50)   CheckedOrganizationName    nvarchar(50)    CheckedTimeZone         nvarchar(50)   CheckedActivity         nvarchar(50)   CheckedStartdate        datetime      CheckedStarttime        nvarchar(50)   CheckedEndDate      datetime      CheckedEndTime      nvarchar(50)   CheckedDuration         int            CheckedIsPaid       nvarchar(50)   CheckedTimeSourceCode       int             CheckedEventType        int             CheckedSample Data (First Row is Header Column First Row does not have "" around the hash this is done so that the hash does not affect formating the hash is part of the field name)"#"TimeRecordID,ExternalID,FirstName,LastName,LastUpdatedDate,LastUpdateTime,LastUpdatedBy,IsApproved,Employee ID#,OrganizationName,TimeZone,Activity,StartDate,StartTime,EndDate,EndTime,Duration,IsPaid,TimeSourceCode,EventType46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Records Merge Activity,2013/06/24,14:53:58,2013/06/24,14:59:00,6,false,4,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,AUX 0,2013/06/24,14:59:00,2013/06/24,15:00:15,1,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:00:15,2013/06/24,15:17:35,17,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,AUX Out,2013/06/24,15:17:50,2013/06/24,15:18:50,1,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Customer on Hold,2013/06/24,15:18:50,2013/06/24,15:19:05,1,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:19:05,2013/06/24,15:21:05,2,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Available,2013/06/24,15:21:05,2013/06/24,15:22:20,1,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:22:20,2013/06/24,15:32:35,10,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Available,2013/06/24,15:32:35,2013/06/24,15:34:06,2,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:34:06,2013/06/24,15:40:52,6,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Available,2013/06/24,15:40:52,2013/06/24,15:41:07,1,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:41:07,2013/06/24,15:43:52,2,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Available,2013/06/24,15:43:52,2013/06/24,15:44:08,1,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:44:08,2013/06/24,15:46:39,2,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Available,2013/06/24,15:46:39,2013/06/24,15:48:24,2,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Phone,2013/06/24,15:48:24,2013/06/24,15:57:54,9,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Available,2013/06/24,15:57:54,2013/06/24,15:59:09,2,true,0,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Records Merge Activity,2013/06/24,15:59:24,2013/06/24,16:00:09,1,false,4,0 46853,3457,Jane,Doe,2013/06/24,14:53:13,,false,545571,Phone Team,GMT,Meeting,2013/06/24,16:00:39,2013/06/24,16:10:10,10,true,0,0SP Code declare @query varchar(1000)declare @max1 intdeclare @count1 intdeclare @filename varchar(100)declare @filepath varchar(500)declare @pattern varchar(100)declare @TableName varchar(128)set @count1 =0set @filepath = '\\susagpwfm01\TRE_Files'set @pattern = '*.csv'set @TableName = 'tbl_Payroll'create table #x (name varchar(200))set @query ='master.dbo.xp_cmdshell "dir '+@filepath+@pattern +' /b"'insert #x exec (@query)delete from #x where name is NULLselect identity(int,1,1) as ID, name into #y from #x drop table #xset @max1 = (select max(ID) from #y)--print @max1--print @count1While @count1 <= @max1beginSet @Filepath = '\\servername\Files_Folder'Set @Pattern = '*.csv'set @count1=@count1+1set @filename = (select name from #y where [id] = @count1)set @query ='BULK INSERT '+ @Tablename + ' FROM "'+ @Filepath+@Filename+'" WITH ( FIELDTERMINATOR = ",",FIRSTROW=2,ROWTERMINATOR = "\n")'--print @queryexec (@query)enddrop table #y |  |  
                                    | MIK_2008Master Smack Fu Yak Hacker
 
 
                                    1054 Posts | 
                                        
                                          |  Posted - 2013-06-28 : 12:17:21 
 |  
                                          | post the print's result of @query variable of the following portion? set @query ='BULK INSERT '+ @Tablename + ' FROM "'+ @Filepath+@Filename+'" WITH ( FIELDTERMINATOR = ",",FIRSTROW=2,ROWTERMINATOR = "\n")'print @queryCheersMIK |  
                                          |  |  |  
                                    | MIK_2008Master Smack Fu Yak Hacker
 
 
                                    1054 Posts | 
                                        
                                          |  Posted - 2013-06-28 : 12:20:32 
 |  
                                          | by the way I don't think double qoutues can be used in Bulk Insert statmentCheersMIK |  
                                          |  |  |  
                                    | MenorelStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2013-07-01 : 09:23:16 
 |  
                                          | Here is the results from the print @query your asked for.  It appears to be building the query correctly.(2 row(s) affected)(1 row(s) affected)(1 row(s) affected)BULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_FilesThe network path was not found." WITH ( FIELDTERMINATOR = ",",FIRSTROW=2,ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 2Incorrect syntax near ','. (1 row(s) affected) |  
                                          |  |  |  
                                    | MenorelStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2013-07-01 : 09:41:35 
 |  
                                          | I just noticed "The network path was not found" statement in there.  Not sure why it is having a problem the folder has 'Everyone'  allowed on it with modify rights so it shouldn't be running into authentication issues. |  
                                          |  |  |  
                                    | MIK_2008Master Smack Fu Yak Hacker
 
 
                                    1054 Posts | 
                                        
                                          |  Posted - 2013-07-01 : 10:09:08 
 |  
                                          | The query should look like following in order to read CSV file using Bulk InsertBULK INSERT tbl_Payroll FROM '\\PathOfTheFile\FolderName\FileName.csv' WITH ( FIELDTERMINATOR = ',',FIRSTROW=2,ROWTERMINATOR = '\n')1) as I mentioned earlier there should be single qoute not double2) you mentioned that its CSV file.. but query youprinted has no sign of information that you're pointing onto a CSV file located on network path .. logically the path should resembles somewhat to --> \\ServerName\FolderName\FileName.CSVCheersMIK |  
                                          |  |  |  
                                    | MenorelStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2013-07-01 : 10:50:38 
 |  
                                          | Ok got the file path thing corrected had left out a "\" in @Filepath.  Here is the updated output from @query.(7 row(s) affected)(1 row(s) affected)(6 row(s) affected)60tbl_Payroll\\susagpwfm01\TRE_FilesBPTRExport__06_26_2013_04_00_09_EXPORTsusagpbdr01.csvBULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_Files\BPTRExport__06_26_2013_04_00_09_EXPORTsusagpbdr01.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 1Incorrect syntax near ','.tbl_Payroll\\susagpwfm01\TRE_FilesBPTRExport__06_27_2013_04_00_09_EXPORTsusagpbdr01.csvBULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_Files\BPTRExport__06_27_2013_04_00_09_EXPORTsusagpbdr01.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 1Incorrect syntax near ','.tbl_Payroll\\susagpwfm01\TRE_FilesBPTRExport__06_28_2013_04_00_08_EXPORTsusagpbdr01.csvBULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_Files\BPTRExport__06_28_2013_04_00_08_EXPORTsusagpbdr01.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 1Incorrect syntax near ','.tbl_Payroll\\susagpwfm01\TRE_FilesBPTRExport__06_29_2013_04_00_08_EXPORTsusagpbdr01.csvBULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_Files\BPTRExport__06_29_2013_04_00_08_EXPORTsusagpbdr01.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 1Incorrect syntax near ','.tbl_Payroll\\susagpwfm01\TRE_FilesBPTRExport__06_30_2013_04_00_07_EXPORTsusagpbdr01.csvBULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_Files\BPTRExport__06_30_2013_04_00_07_EXPORTsusagpbdr01.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 1Incorrect syntax near ','.tbl_Payroll\\susagpwfm01\TRE_FilesBPTRExport__07_01_2013_04_00_07_EXPORTsusagpbdr01.csvBULK INSERT tbl_Payroll FROM "\\susagpwfm01\TRE_Files\BPTRExport__07_01_2013_04_00_07_EXPORTsusagpbdr01.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = "\n")Msg 102, Level 15, State 1, Line 1Incorrect syntax near ','.tbl_Payroll\\susagpwfm01\TRE_Files  (1 row(s) affected) |  
                                          |  |  |  
                                    | MIK_2008Master Smack Fu Yak Hacker
 
 
                                    1054 Posts | 
                                        
                                          |  Posted - 2013-07-01 : 10:56:57 
 |  
                                          | Use Single qoutes not double ... BULK INSERT tbl_Payroll FROM '\\PathOfTheFile\FolderName\FileName.csv'WITH ( FIELDTERMINATOR = ',',FIRSTROW=2,ROWTERMINATOR = '\n')CheersMIK |  
                                          |  |  |  
                                |  |  |  |