Solved

Help needed regarding OSQL utility !!

Posted on 2008-06-25
8
265 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
  • 4
  • 3
8 Comments
 
LVL 83

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
 

Author Comment

by:milani_lucie
ID: 21879339
C:\>Sample.cmd
---------------
--- Setup START
---------------
The syntax of the command is incorrect.
C:\>
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

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 83

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 83

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Incremental load example 2 52
execute a MS SQL script as a schedule SQL job 72 123
Set the max value for a column 7 37
Need some help in Batch file assigning variable value ? 4 66
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now