Bathakarai
Bathakarai

Reputation: 1537

Select Sql to mention column as variable

I did the following to exclude fields (whose type is 'longblob)' from a table.

  1. SELECT group_concat(COLUMN_NAME) into @column FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'document' AND DATA_TYPE NOT IN ('longblob');
  2. select @column from document;

It returns @column value alone rather than displaying value of the field.

Ex: if @column = "id, name", it returns "id, name" alone. Rather than displaying values of id & name respectively.

Upvotes: 0

Views: 74

Answers (1)

Jaugar Chang
Jaugar Chang

Reputation: 3196

Try this:

SELECT group_concat(COLUMN_NAME) into @column 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'document' AND DATA_TYPE NOT IN ('longblob');

set @stmt := concat('select ', @column , ' from document');

select @stmt;

PREPARE stmt FROM  @stmt;

EXECUTE stmt ;

Upvotes: 1

Related Questions