0

Imagine I have a table like this one:

CREATE TABLE marcas
(
  id_marca integer,
  marca character varying
));

I would like to make a query but get the value in every field empty.

I know that could solve it like this:

SELECT '' as id, '' as marca FROM marcas

The problem is that i have plenty of tables and some of them have more than 100 fields... I need a SQL statement that could get all the row fields of a table but empty and in an easy way...

Majid
  • 13,853
  • 15
  • 77
  • 113
Egidi
  • 1,736
  • 8
  • 43
  • 69

3 Answers3

4

Here is one method to get NULL in every column. It uses left outer join with a failing condition:

select t.*
from (select 1 as val
     ) v left outer join
     table t
     on 1 = 0;

EDIT:

If you want to do this in Access, that is a challenge. The expression select 1 as val doesn't work. The following should:

select t.*
from (select max(1) as val
      from table as t 
     ) as v left outer join
     table as t
     on val = 0;

This should also work in Postgres, but it has unnecessary overhead.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • +1 I think this is pretty much as close as you can get. – Joachim Isaksson May 19 '14 at 14:22
  • This query worked perfectly on PostgreSQL but gets an error when trying to execute it in Microsoft Access, it says that 1 = 0 expression is not admitted. – Egidi May 20 '14 at 12:29
  • Thanks Gordon for your answer. I opened a new thread here: http://stackoverflow.com/questions/23760573/ms-access-query looking for a specific solution on this question in ms-access. Your solution for access in my case is not working...although it works again in PostgreSQL. – Egidi May 20 '14 at 13:18
0

If you use

SELECT * FROM MARCAS

you will get a result set of

===================

ID_MARCA | MARCA

===================

This will return zero results but it will show all the columns in the table queried. I can't fully understand what you are trying to achieve.

zbads
  • 135
  • 1
  • 9
0

if you just want the column list and no rows returned then

SELECT TOP 0 * FROM marcas
Jaloopa
  • 722
  • 1
  • 6
  • 21