var sidebar_align = 'right';
var content_container_margin = parseInt('290px');
var sidebar_width = parseInt('270px');
Unanswered: How can I execute SQL query from DOS with parameters?
I believe all of us use SQL Server Management Studio to execute query. As I encountered serious problem with this method, due to typo error by accident, I am wondering if we can run the .sql file from dos and supply parameters with it.
Last edited by tingshen; 02-08-07 at
write a batch fire Test.bat as below (here I am using a trusted connection change it accordingly)
then call it with any sql... like
C:>Test "select * from DBName..TableName"
there could be many other variations of this. check BOL
what shall I do if I have a .sql files which consist of 1000 lines of queries? can it be done in the same way? can pass parameter as variable in the query?
is osql only accepting 1 command each time?
if you are using 2005, you should use sqlcmd.exe. osql.exe is deprecated.
with sqlcmd, you can use parameterized queries where the parameters are environment variables. it's a very powerful technique.
Eg. using sel2.sql
create a batch file to supply parameters e.g.
select dbid,status from sysdatabases
order by dbid
And run it e.g.
echo declare @name varchar(30),@status int >workfile.sql
echo select @name="%1",@status=%2 >>workfile.sql
type sel2.sql >>workfile.sql
osql -n -Ume -Pmypass -Smydb -i workfile.sql
C:\tmp>b1 tempdb 24
(2 rows affected)
Last edited by pdreyer; 02-09-07 at