Solved

Help needed regarding OSQL utility !!

Posted on 2008-06-25
8
271 Views
Last Modified: 2010-05-18
Hi,

I have

D:\Tables
D:\SPs

directories. In D:\Tables, I have create tables scripts. Similarly in D:\SPs, I have create stored procedures scripts.

I have written a .CMD file to install these scripts in the database - creating tables / creating stored procedures etc...

D:\Sample.CMD
============

echo ---------------
echo --- Setup START
echo ---------------

FOR %%f IN
(
  ..\Tables\*.sql
  ..\SPs\*.sql

) DO (echo Executing %%f) & (osql -E -n -S localhost -d Sample -w 8192 -i %%f) || (echo Error %%f & exit)

echo ---------------
echo --- Setup END
echo ---------------

In this script:

Server - localhost
Database - Sample

This is NOT working properly when i double click on this file. Can you please FIX the above program and make it working ? BTW: when i double click on the file, it is showing the results and disappearing quickly. Can you make it stand by until i press any key ?

Thanks
0
Comment
Question by:milani_lucie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 21878935
When testing batch scripts, always open a command prompt and start them from there, not through Explorer; that way, you'll be able to see the error.
Anyway, try this:
echo ---------------
echo --- Setup START
echo ---------------
 
FOR %%f IN ("..\Tables\*.sql" "..\SPs\*.sql" do (
  echo Executing %%f
  osql -E -n -S localhost -d Sample -w 8192 -i "%%f"
  if errorlevel 1 (
    echo Error %%f
    exit /b
  )
)
 
echo ---------------
echo --- Setup END
echo ---------------

Open in new window

0
 

Author Comment

by:milani_lucie
ID: 21879235
echo ---------------
echo --- Setup START
echo ---------------
 
FOR %%f IN
(
      "..\Tables\*.sql"
      "..\SPs\*.sql"
) DO (echo Executing %%f osql -E -n -S localhost -d Sample -w 8192 -i "%%f" if errorlevel 1 (echo Error %%f exit /b))
 
echo ---------------
echo --- Setup END
echo ---------------

This is NOT working. Can you please verify my code ? Please test on your side.

Thanks
0
 

Author Comment

by:milani_lucie
ID: 21879251
I have gone to command prompt and how to open the file ? Just type the name of the file ? Like this ?

C:\>Sample.CMD
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:milani_lucie
ID: 21879339
C:\>Sample.cmd
---------------
--- Setup START
---------------
The syntax of the command is incorrect.
C:\>
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21879371
please try this:
echo ---------------
echo --- Setup START
echo ---------------
 
FOR %%f IN (..\Tables\*.sql ) DO (echo Executing %%f osql -E -n -S localhost -d Sample -w 8192 -i "%%f" if errorlevel 1 (echo Error %%f exit /b))
 
FOR %%f IN (..\SPs\*.sql) DO (echo Executing %%f osql -E -n -S localhost -d Sample -w 8192 -i "%%f" if errorlevel 1 (echo Error %%f exit /b))
      
 
echo ---------------
echo --- Setup END
echo ---------------

Open in new window

0
 

Author Comment

by:milani_lucie
ID: 21879419
Hi angelIII:

Here is the error i have caught...

C:\>Sample.cmd
---------------
--- Setup START
---------------
) was unexpected at this time.
C:\>
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 21879446
Missing closing bracket in the first line, this works:
@echo off
echo ---------------
echo --- Setup START
echo ---------------
 
FOR %%f IN ("..\Tables\*.sql" "..\SPs\*.sql") do (
  echo Executing %%f
  ECHO osql -E -n -S localhost -d Sample -w 8192 -i "%%f"
  if errorlevel 1 (
    echo Error %%f
    exit /b
  )
)
 
echo ---------------
echo --- Setup END
echo ---------------

Open in new window

0
 
LVL 84

Expert Comment

by:oBdA
ID: 21879464
Sorry, forgot to remove the ECHO I put in for testing, currently the osql command will only be shown, not executed. Just remove the capitalized ECHO in front of the osql command.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question