You can use osql or better yet the newer sqlcmd almost interchangeably. I am using osql in this example only because I happened to have a code sample sitting around but in production I am using sqlcmd. Here is a snipped of code out of a larger procedure I use to run update scripts against databases. They are ordered by major, minor, release, build as I name my scripts using that convention to track releases. You are obviously missing all of my error handing, the parts where I pull available scripts from the database, setup variables, etc but you may still find this snippet useful.
The main part I like about using osql or sqlcmd is that you can run this code in ssms, or in a stored procedure (called on a scheduled basis maybe) or from a batch file. Very flexible.
--Use cursor to run upgrade scripts
DECLARE OSQL_cursor CURSOR
READ_ONLY
FOR SELECT FileName
FROM #Scripts
ORDER BY Major, Minor, Release, Build
OPEN OSQL_cursor
FETCH NEXT FROM OSQL_cursor INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF ((@@fetch_status <> -2) AND (@result = 0))
BEGIN
SET @CommandString = 'osql -S ' + @@ServerName + ' -E -n -b -d ' + @DbName + ' -i "' + @Dir + @name + '"'
EXEC @result = master.dbo.xp_cmdshell @CommandString, NO_OUTPUT
IF (@result = 0)
BEGIN
SET @Seconds = DATEDIFF(s, @LastTime, GETDATE())
SET @Minutes = @Seconds / 60
SET @Seconds = @Seconds - (@Minutes * 60)
PRINT 'Successfully applied ' + @name + ' in ' + cast(@Minutes as varchar)
+ ' minutes ' + cast(@Seconds as varchar) + ' seconds.'
SET @LastTime = GETDATE()
END
ELSE
BEGIN
SET @errMessage = 'Error applying ' + @name + '! The database is in an unknown state and the schema may not match the version.'
SET @errMessage = @errMessage + char(13) + 'To find the error restore the database to version ' + @StartingVersion
SET @errMessage = @errMessage + ', set @UpToVersion = the last version successfully applied, then run ' + @name
SET @errMessage = @errMessage + ' manually in Query Analyzer.'
END
IF @name = (@UpToVersion + '.sql')
GOTO CleanUpCursor --Quit if the final script specified has been run.
END
FETCH ENDT FROM OSQL_cursor INTO @name
END