Pass table field as parameter in a precedent

Asked

Viewed 2,023 times

1

I made this precedent to bring records from a table between dates, but I need to pass the date field of the table for the between command to work, follow the code:

DELIMITER //
CREATE PROCEDURE pro_get_gastos(nome_tabela VARCHAR(20), data_ini DATETIME, data_fin DATETIME)
BEGIN
SET @tabela = CONCAT('SELECT * FROM ',nome_tabela,' WHERE ', AQUI TERIA QUE PASSAR O CAMPO DATA, 'BETWEEN', data_ini, ' AND ', data_fin,'''')
PREPARE consulta_gastos FROM @tabela;
EXECUTE consulta_gastos;
END //

How can I fix this?

  • Need to pass the DATE field of which Table ? I don’t understand.

  • All the tables I will use in this database have a date field, the name of the table to be consulted I passed by parameter, however, I need the date field of these tables to use in the "Where CAMPO_DATA between ..."

2 answers

2

Do it like this:

CREATE PROCEDURE pro_get_gastos @nome_tabela sysname,
                                @data_mov    sysname,
                                @data_ini    datetime, 
                                @data_fim    datetime AS
BEGIN 
    DECLARE @sql nvarchar(4000)
    SELECT @sql = ' SELECT * ' +
              ' FROM ' + QUOTENAME(@nome_tabela) +
              ' WHERE ' + QUOTENAME(@data_mov) + ' BETWEEN ' + 
              ' @data_ini AND @data_fim ' 

   EXEC sp_executesql @sql, N'@data_ini datetime, @data_fim datetime', @data_ini, @data_fim
END

Edit: In Mysql you can do so:

delimiter // 
CREATE PROCEDURE pro_get_gastos (IN nome_tabela CHAR(20),
                                 IN data_mov    CHAR(20),
                                 IN data_ini    datetime, 
                                 IN data_fim    datetime)
BEGIN
    SET @sql = CONCAT('SELECT * FROM ', nome_tabela, ' WHERE ', data_mov, ' BETWEEN ', data_ini, ' AND ', data_fim);
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END
//
delimiter ;
  • It gives the following error in the declaration of @table_name: Syntax error: '@table_name' (at text suffix) is not Valid input on this position

  • You can check it this way?

  • That’s what you did using the SQL Server syntax, right? I use mysql

  • 1

    Moment of stupidity on my part. I will update the reply to the Mysql syntax.

0

Creates a parameter beyond the nome_tabela, getting:

CREATE PROCEDURE pro_get_gastos(
    nome_tabela VARCHAR(20), 
    nome_data VARCHAR(20), 
    data_ini DATETIME, 
    data_fin DATETIME)

And in the concatenation you pass equal passed the table

CONCAT('SELECT * FROM ',nome_tabela,' WHERE ', nome_data, ...
  • However I did not want to pass a date, just take the field date of the table I passed as parameter, to be able to use in the "Where CAMPO_DATA between ..."

  • @That’s why a new parameter, man nome_data is equal to your campo_data

  • in this second parameter is not the date value but the column name

  • Okay, but only by declaring this new parameter, it does not solve, because I need this new parameter to be the date field of the chosen table...

  • How are you calling the trial?

  • It would look like this: CALL pro_get_expenditures ('nomeDaTabela', '2015-01-01', '2015-06-06');

  • And what’s so hard to call CALL pro_get_gastos ('nomeDaTabela', 'nomeDaColunaData', '2015-01-01', '2015-06-06');

  • Is not spinning...

  • What error appears in?

  • Error of the syntax... Pensei em fazer assim: 
SET @data_mov = CONCAT(SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = orcpessoal AND TABLE_NAME = nome_tabela AND COLUMN_NAME = data_mov);
Porém não estou conseguindo concatenar de forma correta...

  • Will always be data_mov?

  • Syntax error may be pq you have not created the Procedure correctly with my change

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.