Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use hardcoded list of values in query

I have a list of ISO2 country codes I want to use in a query.
Something like this:

select cou, 128,13, 1
from ('AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', [snip]) as cou

But.. working.

I remember doing such a thing in the past, but I can't find any doc about it anymore. It's a one shot query so I don't mind performance, coding practice or maintainability.

Any ideas?

UPDATE
As Pax noted, it is indeed better practice to have this data in my database for all the good reasons. I understand his opinion because I would answer the same. However, this data already IS in another table, in another database, on another server, on another network..

In order to test my queries I need some quick shot values in a table on this new database. I don't want to configure networks, cross-server queries etc just to test my queries on some real-life data. I hope this explains why I go against the stream for this one shot.

like image 630
Boris Callens Avatar asked Aug 12 '09 06:08

Boris Callens


People also ask

Can we use list in SQL query?

Can we use list in SQL? You can create lists of SQL Query or Fixed Data values .

What is hardcoded in SQL?

Hardcoding is the practice of making code tailored to handle very specific cases. Here are some examples of hardcoding as applies to Drupal: Inserting an SQL query into a . tpl file. Writing a script that queries the database to make some changes to nodes.

How do you add a list to a query?

To create a list query from the selected column, right-click on the column and select 'Add as New Query' option as shown in the picture below. NOTE: You can also use Table. ToList() function in Power Query (M) language for creating a list from a data table.


1 Answers

Values keyword can be used to achieve this.

select col1, 128 col2, 13 col3, 1 col4
from ( values ('AD'), ('AE'), ('AF'), ('AG'), ('AI'), ('AL'), ('AM')) as x (col1)
like image 113
Muthu Avatar answered Oct 04 '22 19:10

Muthu