1

Question:

Is there a way to use names instead of question marks for paramaterized queries? If so, can anyone suggest some material that explains how to do this/the syntax?

A bit more detail:

For example, if I have something like:

INSERT INTO inventory VALUES(?)

Is it possible to have something like this instead that does the exact same thing as the question mark:

INSERT INTO inventory VALUES("prices")

I tried checking to see if it would work myself before posting the question, but it didn't work. So, I thought I'd ask if it was possible.

I feel like if you have a really long query with, let's say 20 parameters, you don't want to have to count question marks to make sure you have enough parameters whenever you change something. Also, I think it might make the code a bit more readable (especially if you have a lot of parameters to keep track of).

I'm rather new to sql, so I am not sure if it makes much of a difference (for this question) if I add that I'm using postgresql.

Note:

There is a similar question here, but it didn't have an answer that was helpful

Community
  • 1
  • 1
JustBlossom
  • 1,259
  • 3
  • 24
  • 53
  • Can you please describe the context? What will evaluate the parametrized SQL statement? Which API are you considering? – mauro Mar 02 '16 at 02:35

1 Answers1

0

I suggest to encapsulate the big query in a function, where you can use parameter names. One example (out of many):

You can even set default values and call the function with named parameters, etc.:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228