Today We Talk About OSQL.
So, what is OSQL?Thus, OSQL is an functional query language.The data model of OSQL is object oriented with three kinds of system entities:
1. Objects
2. Types
3. Functions.
The data model is similar to an ER model with the difference that both entity relationships and attributes which are represented as functions and inheritance among entity.OSQL provide object identifiers which return OIDs as results. Queries are expressed using a SELECT syntax similar to SQL commands.
Difference Between OSQL & ISQL
There is a great deal of overlap between ISQL and OSQL.Both support input scripts, output scripts, and most of the same switch arguments.
OSQL scripts
In these next examples, we will
create and save TSQL scripts, and then run them from OSQL. Query Analyzer is a
standard choice for script creation because of the color coding. Open Query
Analyzer and enter:
USE pubs
GO
SELECT * FROM authors
GO
Save this script to your hard drive,
and then from the command line, enter an OSQL statement using the -i switch to
specify an input file. The authors table should be returned.
OSQL -E -i c:\temp\q1.sql
The results of the query can be
captured to an output file, rather than appearing on the screen. Change the
command line to include the -o parameter, for output.
OSQL -E -i c:\temp\q1.sql -o
c:\temp\resutls.txt
OSQL should create a text output
file. The -u switch can be used to control the output file being either Unicode.
System
Commands
Operating system commands can also
be executed from inside the TSQL script. The key !! is used to specify this.
Change the Query Analyzer script and save it as:
!! dir c:\temp
GO
USE pubs
GO
SELECT * FROM authors
GO
Now our output file will include the
directory listing of the temp folder in addition to the authors' results. Also,
note this script will not run in Query Analyzer. The !! directive is not
supported. Query Analyzer color coding is helpful in code layout, but the script
testing will need to be done from OSQL.
Error
Handling
OSQL supports the RAISERROR command
for returning custom error messages. To use raise error, the database name, id,
error severity and state should be included. Using RAISERROR will cause the script
to terminate. Modify the Query Analyzer script to:
!! dir c:\temp
GO
DECLARE @DBID int
SET @DBID = DB_ID()
DELCARE @DBNAME nvarchar(128)
SET @DBNAME = DB_NAME()
RAISERROR('my error', 18, 127,
@DBID, @DBNAME)
USE pubs
GO
SELECT * FROM authors
GO
Running the script will now output
our directory listing, from the "!! dir c:\temp" command, followed by
the raise error. The remaining script, changing to pubs and selecting from
authors will not occur. RAISERROR will terminate the script.
Leaving a script can also be done by
calling QUIT or EXIT from inside OSQL. Neither of these will return an error
code, but EXIT can execute a statement prior to quitting. For example:
EXIT(SELECT @@ROWCOUNT).
Conclusion
When administering the Microsoft
Desktop Engine, OSQL is a free way to run statements. For standard SQL Server
environments, OSQL can be used to help automate long or repetitive tasks by
reusing scripts. OSQL is also a good choice to run database setup scripts
during application install procedures


This comment has been removed by the author.
ReplyDelete