How to execute multi-stated SQL in QlikView script?

1.6k views Asked by At

I have a MS-SQL query, using temporary table, which from I'd like to get data into QlikView:

declare @oi_im table (tick datetime);

insert into @oi_im
SELECT TOP 360 dateadd(mi, datediff(mi, 0, GETDATE()) - ROW_NUMBER() OVER (ORDER BY ID), 0) as tick
FROM [Messages] timer;

SELECT tick as Created, case when msgs is NULL then 0 else msgs end as msgs
FROM (SELECT TOP 360 tick
FROM @oi_im) timer
LEFT JOIN (SELECT TOP 360 dateadd(mi, datediff(mi, 0, Created), 0) as Created, count(*) as msgs
  FROM [Messages] with(nolock)
  GROUP BY dateadd(mi, datediff(mi, 0, Created), 0)
  ORDER BY Created desc) msgslist ON msgslist.Created=timer.tick
ORDER BY timer.tick desc

My first try was to just copy this text into QlickWiev "Edit Script" window, but the problem is - script throws statements into SQL one by one. After making first statement (make temporary table) MS SQL server loses the context, and on second statement produces error "I don't know any @oi_im"

Next try is to wrap this text into stored procedure:

SQL execute sp_executesql N'declare @oi_im table (tick datetime);


insert into @oi_im
SELECT TOP 360 dateadd(mi, datediff(mi, 0, GETDATE()) - ROW_NUMBER() OVER (ORDER BY ID), 0) as tick
FROM [Messages] timer with(nolock);


SELECT tick as Created, case when msgs is NULL then 0 else msgs end as msgs
FROM (SELECT TOP 360 tick
FROM @oi_im) timer
LEFT JOIN (SELECT TOP 360 dateadd(mi, datediff(mi, 0, Created), 0) as Created, count(*) as msgs
  FROM [Messages] with(nolock)
  GROUP BY dateadd(mi, datediff(mi, 0, Created), 0)
  ORDER BY Created desc) msgslist ON msgslist.Created=timer.tick
ORDER BY timer.tick desc
'

It produces no error, but unfortunately returns no data at all.

So the question is: how to execute multi-stated SQL in QlikView script, while first SQL statement is not a SELECT?

0

There are 0 answers