0

It is necessary to write a procedure that makes a request, which, in turn, should output all the objects of a particular column. Here is what I tried:

CREATE PROCEDURE AttributeRequest(n CHAR(200))
begin
  SELECT n FROM table;
end

But this variable is perceived as the name of the column itself and nothing comes out. Tell me how to make such a request by the attribute of the object, please

I searched a bit, read the answers below, and I managed to implement this task in the following way:

CREATE PROCEDURE AttributeRequest(n CHAR(200))
begin
    SET @t =CONCAT("SELECT ",n ," FROM table");
    PREPARE e FROM @t;
    EXECUTE e;
end
Midel
  • 13
  • 3
  • You can write the dynamic query as an option see below link which might help you https://stackoverflow.com/questions/23178816/mysql-dynamic-query-in-stored-procedure – Rajat Jaiswal May 01 '20 at 15:25

1 Answers1

0

you will have to use dynamic sql for this

CREATE PROCEDURE AttributeRequest(@n CHAR(200)) AS
begin
  declare @query varchar(1000);
  set @query = 'select ['+@n+'] FROM table;
  exec AttributeRequest @query
end

then you can excute it ,

exec AttributeRequest 'columnname'
Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
awen
  • 79
  • 1
  • 8