Tags

,


I wrote an article some time back called SQL Server- Running SQL script Files using osql !! which showed how we can execute the SQL Script files through command Lines.

Today I am going to show another way of doing it. We can do the same thing with sqlcmd command. osql version was introduced in SQL Server 2000 version whereas sqlcmd is recently added in SQL Server 2005. sqlcmd has all the features of osql and also it has many added features so we should always try to use sqlcmd for our own convenience.

In this article I am going to show the same example that I used in the previous article but I am going to execute the script file through sqlcmd.

— 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 “sql_cmd.sql“. In my case I have saved script file in H:\ drive.

Go to command prompt and pass the following command:

sqlcmd -U your_username -P your_password -S server_name -d database_name -i h:\sql_cmd.sql

So we have seen that we can run the script file through sqlcmd. As I suggested before please try to use sqlcmd than using osql or isql commands as sqlcmd is recently introduced and it has all the features of osql commands and also has some added features that we can use.

So that is it from my side for today. I hope you have enjoyed the piece of information.

Keep Learning!!

Thanks

Subhro Saha

Advertisements