Tags


We can use osql to execute database script files, which can be text files containing a mix of Transact-SQL statements and osql commands. osql has no interface. It will only accept typed command line or a saved script. osql works with statements and commands in the script file in a manner very similar to the how it works with statements and commands entered interactively. The main difference is that osql reads through the input file without pause instead of waiting for a user to enter the statements and commands.

— Creating a table

CREATE TABLE dbo.test
(
FName VARCHAR(20),
LName VARCHAR(20)
)

— Inserting records in the table created above

INSERT INTO test VALUES (‘AAA’,‘BBB’)
INSERT INTO test VALUES (‘FFF’,‘CCC’)
INSERT INTO test VALUES (‘DDD’,‘EEE’)

In a notepad, pass the following command SELECT * FROM Test and save the file as “a.sql“. In my case I have saved script file in H:\ drive.

Go to command prompt and pass the following command:

osql -U your_username -P your_password -S server_name -d database_name -i h:\a.sql

It will show the records in the following way:

Thus you successfully ran the SQL script that was saved earlier in the command prompt without even opening the script file.

Keep coding and keep Learning!!

Thanks

Subhro Saha

Advertisements