gbrian / grafana-simple-sql-datasource

Grafana SQL datasource
MIT License
62 stars 20 forks source link

Storred Procedure Query #27

Closed stracho closed 6 years ago

stracho commented 6 years ago

I have a problem with SQL Query in Grafana. I have to query database with storred procedure.

DECLARE @return_value int

EXEC    @return_value = [dbo].[ReadProcessedP]
        @periodStart = N'now-1m',
        @periodEnd = now,
        @resampleInterval = 60,
        @parameters = ' ',
        @column1 = N'Ch_5'

SELECT  'Return Value' = @return_value

GO`

SQL Query:

ReadProcessedP 'now-1m', 'NOW', '60', '', 'CH_5'

When I try to query in grafana query inspector shows:

xhrStatus:"complete"

request:Object
method:"POST"

url:"api/datasources/proxy/2"

data:Object
type:"query"

body:Object
timezone:"browser"

panelId:2

range:Object

rangeRaw:Object

interval:"20s"

intervalMs:20000

targets:Array[1]

format:"json"

maxDataPoints:1184

scopedVars:Object

cacheTimeout:undefined

url:null

response:Array[1]
0:null

But when I do this in MS SQL Managmenet Studio response is 41,105899810791 Can Grafana send "clear" query to MS SQL Database as Table or Graph ?

buiductri commented 6 years ago

Sorry but your question seems unclear to me. I need to clarify them before knowing what you really want.

  1. 
    DECLARE @return_value int

EXEC @return_value = [dbo].[ReadProcessedP] @periodStart = N'now-1m', @periodEnd = now, @resampleInterval = 60, @parameters = ' ', @column1 = N'Ch_5'

SELECT 'Return Value' = @return_value

GO


Is this code put in the grafana panel? If yes then I don't know where the "now-1m" and "now" come from. They should be built-in template "$from" and "$to" in order to parse the time range.
The time parsed from "$from" and "$to" can be converted directly into `DATETIME` type.

2. 
`ReadProcessedP 'now-1m', 'NOW', '60', '', 'CH_5'`
You execute this in SSMS right? And it returns `41,105899810791`?
So I assume your SP is selecting a value (maybe FLOAT type) and return another value (like `RETURN 1;` for example), right?
If that's the case, the first code will only return the `41,105899810791` value and not the 'Return Value' (this I am not sure, but it might be the case).

You shouldn't use FLOAT type in T-SQL and in Grafana also, because they cannot parse for some reasons. See https://github.com/gbrian/grafana-simple-sql-datasource/issues/23.

And one more point you should take a look at, that is the alias of returned columns. See https://github.com/gbrian/grafana-simple-sql-datasource/issues/21.

If your panel is single stat, make sure the only one row returned.

I can only go this far, good luck. 😄 
gbrian commented 6 years ago

Sorry, automatically closing old issues after several fixes. Please reopen if the problem persists.