Is it possible to create a dynamic table name in statement of jdbc input plugin in Logstash?

input {
  jdbc {
    jdbc_connection_string => "jdbc:sqlserver://ip_address:1433;databaseName=database_name;encrypt=true;trustServerCertificate=true;"
    jdbc_user => "userxxxx"
    jdbc_password => "passxxxx"
    jdbc_driver_library => "logstash-core/lib/jars/mssql-jdbc-12.2.0.jre8.jar"
    jdbc_driver_class => ""
    statement => "SELECT * FROM DeviceLogs_5_2023 WHERE DeviceLogId > :sql_last_value ORDER BY DeviceLogId ASC"
    schedule => "* * * * *"
    tracking_column => "devicelogid"
    use_column_value => true
    type => "devicelogs"

Here, in statement table name is DeviceLogs_5_2023. But it will change according to the present month and year.

Is there any way to make the name of table name in statement dynamically?
Or any other methods?

1 Like

Hey @rabih and welcome to the community!

If you want that kind of logic, you may be better off creating a stored procedure in mssql which has some dynamic SQL to produce the table name.

Then in logstash you can call statement => "EXEC dbo.stored-proc-name @lastvalue = :sql_last_value"

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.