Re: osql in a cmd file, with SQL statements coming from the same cmd f

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: David Gugick (davidg-nospam_at_imceda.com)
Date: 12/08/04


Date: Tue, 7 Dec 2004 20:45:53 -0500

forestial wrote:
> I want to run osql in a command file. Instead of having the SQL
> statements in a separate .sql file, using "osql -i file.sql..." I
> want to have the SQL statements right there in the command file.
> This way my command file is self-contained; only one file to worry
> about instead of separate cmd and sql files.
>
> In unix (or more precisely in the bash shell) this would be done by
> what is called a "here document". Conceptually:
>
> osql <<END_OF_SQL
> select * from customers
> select * from suppliers
> END_OF_SQL
>
> Any way to do this with OSQL, or by some CMD.EXE trick?

Sure. You can create a CMD file to run the batch with contents like
this: Substitute -E to use a trusted connection which is recommended
over putting user id and password in the file. Use a capital Q to exit
OSQL immediately. Separate batches with Go and use -O for an output
file.

osql -Uuser -Ppassword (or -E) -Sserver -Q"Select id from sysobjects go
select id from sysindexes" -ooutput.txt

-- 
David Gugick
Imceda Software
www.imceda.com 


Relevant Pages

  • Re: Cannot get OSQL line to work for SharePoint
    ... I'm sure other people have moved their SharePoint files from one drive to ... And do you need the OSQL command to execute automatically and quit? ... Because you can use it as an interactive query tool too, which might be easier for you to work with. ... then you enter a "go" statement to submit the query to SQL Server: ...
    (microsoft.public.windows.server.sbs)
  • Re: Can you use osql in DTS?
    ... Don't know if I mentioned it, but I am on SQL 7. ... > DTS can use Dynamic Properties tasks. ... > Can the osql be used in DTS? ... > @chartime into a stored procedure that I would call. ...
    (microsoft.public.sqlserver.dts)
  • Re: Can you use osql in DTS?
    ... Don't know if I mentioned it, but I am on SQL 7. ... > DTS can use Dynamic Properties tasks. ... > Can the osql be used in DTS? ... > @chartime into a stored procedure that I would call. ...
    (microsoft.public.sqlserver.programming)
  • Re: execute a sql file
    ... > beside ISQLW, OSQL, and ISQL utilities, ... ISQL sucks! ... OSQL is better than ISQL, ... But if your SQL is slow, then the tools are not making it any ...
    (microsoft.public.sqlserver.programming)
  • Problem running DTS package automatically
    ... then uses dts to insert the data into a SQL 2000 database. ... or from within Windows Explorer. ... command file runs, it does the ftp fine, but it will not execute the ... According to both the dbadmin and the scheduler, ...
    (microsoft.public.sqlserver.dts)