Link to home
Start Free TrialLog in
Avatar of Mo
Mo

asked on

Passing variable into Procedure

Hello;

I would like to include the ' when I pass in a variable into function.  I am using PostgresSQL.

A Date is passed into a stored procedure; I use this date to get the MonthID so I can run my query.  The issue I have is when I am either declaring the MonthID or passing it into my script it does not include the ' '

Works - but I need to use a variable as I pass this in three times
WHERE   MonthID = ' || to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM') || '

Does not work
WHERE         MonthID =  ' || to_char(l_month_id) || '
Avatar of ste5an
ste5an
Flag of Germany image

Your question indicates that you try to pass in fragments of a dynamic SQL statement!?

This is the wrong approach. Just pass in the date. Do the dynamic SQL in the procedure.

For further help, we need more information. Especially we need to see the entire procedure (declaration, important parts).
Avatar of Mo
Mo

ASKER

Thanks for your response; I only want to pass in the monthID; this works perfect when I use the below: WHERE   MonthID = ' || to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM') || '

My question is; how do I put the extra quotation around a variable:
'202104' as opposed to 202104

Thanks a million
WHERE   MonthID = ' || to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM') || '

Open in new window

is part of some dynamic SQL.

Your posts do not explain why and where you need extra quotes. And without seeing the rest as I've asked for, I  cannot tell where you need one.
Try that please.
WHERE   MonthID = '''' || to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM') || ''''

Open in new window

https://www.db-fiddle.com/f/4jyoMCicNSZpjMt4jFYoz5/1667
Your fiddle in combination does not tell where and why, thus how your quotes are needed. It is just not enough information, your questions scenario is still incomplete. And you've given too few information to guess what you need.
In question:
A Date is passed into a stored procedure; I use this date to get the MonthID so I can run my query.  
The issue I have is when I am either declaring the MonthID
or
passing it into my script it does not include the ' '

In later comment:
 I only want to pass in the monthID

I'm a little confused by the inconsistences above, please choose just one of these:
  1. Do you want to pass the Date ONLY?
  2. Do you want to pass the Date AND the MonthID?
  3. Do you want to pass ONLY the MonthID?

Then also identify the data type of the parameter(s)  are they varchar? date? integer?

Plus, why are you trying to use an interval with the TO_CHAR function?
Do you need to ADD 1 MONTH? Then convert that DATE into YYYMM format?
(if adding 1 month you probably should pass a valid DATE into your procedure)

Honestly, we really do not have enough information to help thoroughly. I have to agree with Stefan that it would be so much easier if we saw the procedure code so we can make informed recommendations.

Avatar of Mo

ASKER

Thanks all for your input:

  1. Do you want to pass the Date ONLY?
No
  • Do you want to pass the Date AND the MonthID?

  • Do you want to pass ONLY the MonthID?
I need to pass in MonthID only '202104'  but need to read the date that was passed in so I can subtract one month from it.

Then also identify the data type of the parameter(s)  are they varchar? date? integer?

Plus, why are you trying to use an interval with the TO_CHAR function?
Do you need to ADD 1 MONTH? Then convert that DATE into YYYMM format?
I need to subtract one month from the date that is passed into the stored procedure.

||'' '|| to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM')  ||' ''||

I have tried all different combinations; but nothing works for me.  

If I do the below:  ' || to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM') || '
it reads the monthid as an integer; but the variable I am passing it into is a varchar
What is the syntex to add ' around the variable.  I am new to Postgres

NOTE: I don't like publishing my full script based on security risks.  Hope this clarifies the situation.

Thanks 
ASKER CERTIFIED SOLUTION
Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mo

ASKER

 '''||to_char(batch_dt::DATE-INTERVAL '1 MONTH', 'YYYYMM')||'''

Open in new window


The above works; very confusing how many characters you need to put into a script to input quotations....

Thanks for all your help