Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am wondering if it is possible to pass parameters to an SQL script from within sybase's isql utility.

For example, I'd like to store a select statement in the file the_script.sql that would look something like

select
   col_1,
   col_2,
   etc
from
   table
where
   cond1 > $param1 and
   cond2 < $param2

Then, with that file, I'd like to "execute" it from isql with

:r the_script.sql 900 20

with the expectation that $param1 is repleaced with 900 and $param2 with 20.

Is there a possibility to achieve what I want?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Looking at your use case, it looks as if you want a stored procedure, so here's one I wrote for you:

CREATE PROC the_script
(
   @param1 int = null,
   @param2 int = null
)
AS
BEGIN
   select
      col_1,
      col_2,
      etc
   from
      table
   where
      cond1 > @param1 and
      cond2 < @param2
END

Now, being in isql, you can run it:

exec the_script 900, 20

Regards.

share|improve this answer

I don't think it can be done positionally from within isql. Using a shell script wrapper, you could do something like

#!/bin/bash

PARAM1=$1
PARAM2=$2

isql -u whoever -p whatever -s myserver << EOF
select * from mytable where mycolumn > ${PARAM1} and mycolumn < ${PARAM2}
go
EOF
share|improve this answer
    
Thanks for your reply. I was aware of this possibility. But I am looking for a method to manually execute "configurable" scripts from isql. –  René Nyffenegger May 25 '11 at 8:27

The only tool I know of to allow you to do that is SQSH. It provides a language that is a blend of shell script and SQL. So you can get your (sqsh) shell script to read parameters from the command line or a file etc. and pass them in to sql batches written in-line with the shell script. http://www.sqsh.org/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.