How to prompt an SQL query user to input information

Vbasic4now picture Vbasic4now · Jun 25, 2015 · Viewed 69.3k times · Source

I have a query that I wrote in SQL server that will be run mainly by people who don't know SQL, and there are two areas that have to have a different string or date entered each time the query is run. As of right now, I just wrote it so that you enter the information at the top of the query and its stored as a variable. Is there a way that I can get SQL to prompt the person running the query to enter the data? below is an excerpt of the code that has what I am talking about in it.

declare 
/*ENTER ACCOUNTING MONTH*/     
   @amon VARCHAR(2) = '05',
/*ENTER INVOICE DATE IN MM/DD/YYYY FORMAT*/
   @invdate DATE = '05/31/2015'
~~
rest of the code
~~
declare @sumA numeric(25, 5), @sumB numeric(25, 5), @ratio numeric(25, 5)
select @sumA = sum(amnt) from accnt where accno = '1152'
select @sumB = sum(amnt) from acc1152
update acc1152 set amnt = amnt * (@sumA/@sumB),
amon = @amon,
invdate = @invdate,
ven = '1152',
code = '1152',
invno = 'INVENTORY'

so is it possible for SQL to prompt the user to type in the value for @amon and @invdate? other than me just having the comment line telling them to do so?

Answer

Giorgi Nakeuri picture Giorgi Nakeuri · Jun 25, 2015

In case you can not do an application, you have no developers etc etc, you have one way - make a stored proc:

create stored procedure spDoSomeJob
@amon VARCHAR(2),
@invdate DATE
as
begin

    ~~
    rest of the code
    ~~
    declare @sumA numeric(25, 5), @sumB numeric(25, 5), @ratio numeric(25, 5)
    select @sumA = sum(amnt) from accnt where accno = '1152'
    select @sumB = sum(amnt) from acc1152
    update acc1152 set amnt = amnt * (@sumA/@sumB),
    amon = @amon,
    invdate = @invdate,
    ven = '1152',
    code = '1152',
    invno = 'INVENTORY'

end

Deny any activity permissions for users except just running this procedure. Execute it like:

exec spDoSomeJob  @amon = '05', @invdate = '05/31/2015'

At least you will be sure that no user can occasionally corrupt something... And if you will not supply values to parameters of stored procedure it will prompt you to do this unless you have no default values for those parameters. It seems to me like the best workaround for your case.