Search
 
SCRIPT & CODE EXAMPLE
 

SQL

TSQL Find csv file in folder

    --BULK INSERT MULTIPLE FILES From a Folder 

    --a table to loop thru filenames drop table ALLFILENAMES
    CREATE TABLE ALLFILENAMES(WHICHPATH VARCHAR(255),WHICHFILE varchar(255))

    --some variables
    declare @filename varchar(255),
            @path     varchar(255),
            @sql      varchar(8000),
            @cmd      varchar(1000)


    --get the list of files to process:
    SET @path = 'C:Dump'
    SET @cmd = 'dir ' + @path + '*.csv /b'
    INSERT INTO  ALLFILENAMES(WHICHFILE)
    EXEC Master..xp_cmdShell @cmd
    UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null


    --cursor loop
    declare c1 cursor for SELECT WHICHPATH,WHICHFILE FROM ALLFILENAMES where WHICHFILE like '%.csv%'
    open c1
    fetch next from c1 into @path,@filename
    While @@fetch_status <> -1
      begin
      --bulk insert won't take a variable name, so make a sql and execute it instead:
       set @sql = 'BULK INSERT Temp FROM ''' + @path + @filename + ''' '
           + '     WITH ( 
                   FIELDTERMINATOR = '','', 
                   ROWTERMINATOR = ''
'', 
                   FIRSTROW = 2 
                ) '
    print @sql
    exec (@sql)

      fetch next from c1 into @path,@filename
      end
    close c1
    deallocate c1


    --Extras

    --delete from ALLFILENAMES where WHICHFILE is NULL
    --select * from ALLFILENAMES
    --drop table ALLFILENAMES
Comment

TSQL Find csv file in folder

    --BULK INSERT MULTIPLE FILES From a Folder 

    --a table to loop thru filenames drop table ALLFILENAMES
    CREATE TABLE ALLFILENAMES(WHICHPATH VARCHAR(255),WHICHFILE varchar(255))

    --some variables
    declare @filename varchar(255),
            @path     varchar(255),
            @sql      varchar(8000),
            @cmd      varchar(1000)


    --get the list of files to process:
    SET @path = 'C:Dump'
    SET @cmd = 'dir ' + @path + '*.csv /b'
    INSERT INTO  ALLFILENAMES(WHICHFILE)
    EXEC Master..xp_cmdShell @cmd
    UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null


    --cursor loop
    declare c1 cursor for SELECT WHICHPATH,WHICHFILE FROM ALLFILENAMES where WHICHFILE like '%.csv%'
    open c1
    fetch next from c1 into @path,@filename
    While @@fetch_status <> -1
      begin
      --bulk insert won't take a variable name, so make a sql and execute it instead:
       set @sql = 'BULK INSERT Temp FROM ''' + @path + @filename + ''' '
           + '     WITH ( 
                   FIELDTERMINATOR = '','', 
                   ROWTERMINATOR = ''
'', 
                   FIRSTROW = 2 
                ) '
    print @sql
    exec (@sql)

      fetch next from c1 into @path,@filename
      end
    close c1
    deallocate c1


    --Extras

    --delete from ALLFILENAMES where WHICHFILE is NULL
    --select * from ALLFILENAMES
    --drop table ALLFILENAMES
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql find char in string 
Sql :: select all column 
Sql :: sql create text column limited values 
Sql :: oracle allow space to user 
Sql :: how to make trigger in phpmyadmin 
Sql :: Test SQL snippets 
Sql :: sql eomonth(getdate) 
Sql :: sqlalchemy database uri 
Sql :: code to move ietms from one table to another myswl 
Sql :: sql agent jb is enabled query 
Sql :: mysql Puede ser solamente un campo automatico y este debe ser definido como una clave 
Sql :: get db connection detail from sql developer profile 
Sql :: APEX elapsed time 
Sql :: sql equal then arrow 
Sql :: what is unsigned mysql 
Sql :: REFRESH command materialized view pgadmin example 
Sql :: how to create an SQL save method in ruby 
Sql :: synapse sql table set pk 
Sql :: fill column postgresql 
Sql :: which takes more space in a database a datetime or separate date and time columns? 
Sql :: Time difference in hh:mm:ss 
Sql :: Creating a comment and reply system PHP and MySQL 
Sql :: syntac for creating view in dbbrowser 
Sql :: power bi find all ids not in other tables 
Sql :: dbms interview questions 
Sql :: what is database username and password in mysqliconnect 
Sql :: c# execute transact sql 
Sql :: strftime format sqlite 
Sql :: ALTER TABLE myTable RENAME CONSTRAINT PK_constraint to PK01_Constraint; 
Sql :: select a row include list of array with join two table SQL 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =