All Apps and Add-ons

Splunk DB Connect: How to use a macro with dbxquery?

burwell
SplunkTrust
SplunkTrust

I am trying to use a macro with dbxquery.

1) I have one macro, called macro1 that returns something like the following using strftime. It's an eval macro. That part works fine.

"Job='201611251010' OR job='201611251020'"

2) I want to use macro1 in another macro, macro2, for use with dbxquery

"dbxquery connection=mydb query=\"select * from mytable where field1 like 'foo%' AND (".`macro1`.") limit 1\""

And then use by

|`macro2`

I always get that macro2 is expected to return a string.

0 Karma
1 Solution

vasanthmss
Motivator

Use sub query to build your query string, there you can use any macros.

something like this...

|dbxquery connection=mydb [|stats c | eval query="<build your query with macro / anything..>" | return query] 

Hope this will helps you!!!!

V

View solution in original post

vasanthmss
Motivator

Use sub query to build your query string, there you can use any macros.

something like this...

|dbxquery connection=mydb [|stats c | eval query="<build your query with macro / anything..>" | return query] 

Hope this will helps you!!!!

V

burwell
SplunkTrust
SplunkTrust

Hello. I got this to work. Thanks!

The trick for me was that I could NOT put together a string using "string1".macro1."string"
Instead I made a string with a dummy value (xxxx) and used replace.

Here's what I did:

1) macro1 IS eval based (because I use strftime )

definition="eval job=\""."<my random string etc>"

2) macro2 is NOT eval based

definition="`macro1` | eval query="select * from table WHERE xxxx" | eval query=replace(query,"xxxx",job) "

Then to use:

|dbxquery connection=mydb  [| stats c| `macro2` |return query] | stats count
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...