0 Replies - 895 Views - Last Post: 13 August 2017 - 04:44 AM Rate Topic: -----

#1 pineapplePie   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 23-January 12

how to get a table name using cmd?

Posted 13 August 2017 - 04:44 AM

Hi all.

I have so many sql files which only have create sentences. Now, I need to first drop the tables before creating them.

What Im having trouble is I cant get the name of a table using a batch.

e.g. Before SQL
create table HOGEHOGE
    id char(20),
    name char(10)  

e.g After SQL(this is what Id like to have)
create table HOGEHOGE 
    id char(20), 
    name char(10)   

@echo off

set inputFile=%1
set outputFileName=%2
set fileExtension=.txt


rem delete the file if already existed
if exist %outputFileName%%fileExtension% (
    del %outputFileName%%fileExtension%

rem get the first line of the file
rem the line "set" gives a message "The system cannot find the file specified."
set /p firstLine=< %inputFile%

rem extract a key word
setlocal enabledelayedexpansion
for /f "tokens=3 delims=" %%a in ('findstr /b /c:"create table" "%inputFile%"') do (
    rem set ID=%%a
echo %ID%

rem insert a sentence
echo DROP TABLE %ID% CASCADE CONSTRAINTS PURGE > %outputFileName%%fileExtension%
echo / >> %outputFileName%%fileExtension%

for /f "delims=;" %%b in (%inputFile%) do (
    echo %%b >> %outputFileName%%fileExtension%

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1