Skip to main content
Announcements
NEW: Seamless Public Data Sharing with Qlik's New Anonymous Access Capability: TELL ME MORE!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

SQL Stored Procedure into QlikView

Hi,

i'm currently trying to get data with stored procedures into Qlikview.
Is this possible?

Thanks in advance

Elzo

64 Replies
Not applicable
Author

Wade,

The last statement in a stored procedure where a #Temp table is used is the drop table statement. If the table is dropped before it is queried, then the table is gone and there's nothing to query against.

If this doesn't help, post and example and maybe I can see something to help you out. I've been using stored procedures in QlikView load scripts for over a year, most of my load scripts use them. They run like a champ.

Allen

wadesims
Contributor III
Contributor III

Allen:
I agree that the example that you provided would not work because of the drop tables, but we have been unable to get any stored procedure that has multiple steps and which ends in a select statement to return rows.
We are currently running QlikView version 9.00.7469.8 SR4 64-bit Edition (x86) and SQL Server 2005. We connect to the database using SQLOLEDB.
There are some examples in the thread above, but here is an example of one that fails to return any rows although the stored procedure does execute correctly:


SQL to create table used in example:

USE [SketchPad]
GO
/****** Object: Table [dbo].[perm_table_items] Script Date: 06/22/2010 15:33:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[perm_table_items](
[perm_table_key] [int] IDENTITY(1,1) NOT NULL,
[perm_table_item_description] [varchar](50) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

SQL to create stored procedure used in example:

USE [SketchPad]
GO
/****** Object: StoredProcedure [dbo].[select_from_perm_table] Script Date: 06/22/2010 15:36:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[select_from_perm_table] AS

TRUNCATE TABLE dbo.perm_table_items;

INSERT INTO dbo.perm_table_items VALUES ('This is the first item');
INSERT INTO dbo.perm_table_items VALUES ('This is the second item');
INSERT INTO dbo.perm_table_items VALUES ('This is yet another item');

SELECT * FROM dbo.perm_table_items;

Load script in QlikView:

LET vDT0105_SketchPad = '[Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=SketchPad;Data Source=DT0105;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=DB07;Use Encryption for Data=False;Tag with column collation when possible=False] (mode is write)';

CONNECT TO $(vDT0105_SketchPad);

PermTableItems:
SQL EXECUTE dbo.select_from_perm_table;

Could you provide an example of one that works?

Not applicable
Author

Wade,

I use an ODBC connection from a MS Server 2003 to a MS SQL Server 64-bit on a different box. When I develop an application I do it on my work station that has the same ODBC connections by name so when I save it over to our production server it all works without any modifications. We have a developer's SQL user which we use for QlikView and MS SSRS. Our techs set up the user to allow for admin level. The first time it was set up, we had to get them to modify it because it was set for read only access. This maybe something you might need to consider, since you are unable to write to both #Temp Tables and Perm Tables.

Example:

This has run every week without fail for over a year.

/****** Object: StoredProcedure [dbo].[Leave_Bal_Term] Script Date: 06/23/2010 08:06:42 ******/

SET ANSI_NULLS ON

GO

SET



QUOTED_IDENTIFIER ON

GO

CREATE



procedure [dbo]. [Leave_Bal_Term]

@ts_date



datetime

AS

declare



@FY_CD varchar(4)

select



@FY_CD = year_no_cd from TS_SCHEDULE_PERIOD where end_dt =@ts_date



--pull in hours for currrent period for calculation

create

TABLE #tmp





(EMPL_ID VARCHAR(20), LV_TERM_DT DATETIME, LV_TYPE_CD VARCHAR(4),BIWKHOURS DECIMAL(18,2), Lv_Bal DECIMAL(18,2))



INSERT



#TMP



select empl_id, TERM_DT, ' ',0.0,0.0 from empl where empl_status_cd = 'ACT' and term_dt is not null



CREATE TABLE #TMPHOURS



(EMPL_ID VARCHAR(20), BIWKHOURS DECIMAL(18,2))



INSERT



#TMPHOURS



SELECT EMPL_ID, SUM(ENTERED_HRS) FROM ts_ln

WHERE ts_dt = @ts_date and pay_type = 'R'

AND EMPL_ID IN (select empl_id from empl where empl_status_cd = 'ACT' and term_dt is not null)



GROUP BY EMPL_ID



CREATE TABLE #TMPLVBAL



( EMPL_ID VARCHAR(20), LV_TYPE_CD VARCHAR(4), BEG_BAL DECIMAL(18,2), YTD_ACCRD_HRS DECIMAL(18,2), YTD_USED_HRS DECIMAL(18,2), CRNT_USED_LV_HOURS DECIMAL(18,2))

INSERT



#TMPLVBAL

SELECT EMPL_ID, lv_type_cd, beg_bal_hrs , ytd_accrd_hrs , ytd_used_hrs, 0.0

FROM EMPL_LV_BAL

WHERE lv_id not in ('HOL', 'UPL', 'FML') and lv_yr_no = @FY_CD

and empl_id in (select empl_id from empl where empl_status_cd = 'ACT' and term_dt is not null)

UPDATE



#TMPLVBAL

SET CRNT_USED_LV_HOURS = ENTERED_HRS

FROM TS_LN d

WHERE #TMPLVBAL. EMPL_ID = d. EMPL_ID AND acct_cd in ('95000-021' , '95000-005') AND TS_DT = @TS_DATE

UPDATE



#TMP

SET BIWKHOURS = #TMPHOURS. BIWKHOURS

FROM



#TMPHOURS

WHERE #TMP. EMPL_ID = #TMPHOURS. EMPL_ID

UPDATE



#TMP

SET LV_BAL = BEG_BAL + YTD_ACCRD_HRS - YTD_USED_HRS - CRNT_USED_LV_HOURS,

LV_TYPE_CD = #TMPLVBAL. LV_TYPE_CD

FROM



#TMPLVBAL

WHERE #TMP. EMPL_ID = #TMPLVBAL. EMPL_ID

UPDATE



#TMP

SET LV_BAL = LV_BAL + ((BIWKHOURS/80)*4)

where LV_TYPE_CD = 'PERSONAL'

UPDATE



#TMP

SET LV_BAL = LV_BAL + ((BIWKHOURS/80)*3)

where



LV_TYPE_CD = 'PS'

UPDATE



#TMP

SET LV_BAL = LV_BAL +3

where LV_TYPE_CD = 'LS'

SELECT * FROM #TMP ORDER BY EMPL_ID

DROP TABLE #TMP

DROP TABLE #TMPHOURS

DROP TABLE #TMPLVBAL



GO

Load script

ODBC Connection string

Leave_Bal:

SQL Execute Leave_Bal_Term '6-22-2010';

Hope this helps

Allen

Not applicable
Author

Allen,

I too have been using SQL Server stored procedures for years and in multiple enviornments without issue in the past. The drop statement is not the problem. The procedure fails with or without. I have two nearly identical procedures. They pull information from different views/tables but the logic is the same. create temp table, execute another proc and return results to that temp table, select from temp table, drop temp table. 1 fails and 1 does not. I'm not sure that the problem has anything to do with the use of temp tables. They both execute in SQL perfectly. To get the failing proc to execute successfully in Qlikview without connection failure, I took out the final select and directly retured the other procedures execution results. While this works it provides zero insight as to why this and only this stored procedure is failing when the others are not.

Does anyone know if # of records returned or overall execution time be a factor? Any other ideas.

Thank you,

Brittany

Not applicable
Author

Brittany,

If I read your post correctly, it's the final Select that's giving you and issue? If so please post the select statement.

Thanks

Allen

wadesims
Contributor III
Contributor III

Brittany, Allen:

Thank you so much for your input and examples. I changed the QlikView script to use an ODBC connection rather than using OLEDB and the same stored procedure now returns rows. Here is the revised QlikView load script:

ODBC CONNECT TO DT0105_SketchPad;

PermTableItems:
SQL EXECUTE dbo.select_from_perm_table;

So far, it looks like there is some difference in the behavior within QlikView depending on whether you are using ODBC or OLE DB to connect to the database.

wadesims
Contributor III
Contributor III

As a follow up (I realize that I was a little vague just to imply that ODBC works and OLEDB doesn't in my last post) here are some of the details associated with the different connection methods I was using. This displays better in a non-kerned font such as courier new.

| Attribute | OLDDB | ODBC |
| Provider | SQLOLEDB.1 | 03.86.3959 |
| Integrated Security | SSPI | Yes |
| Initial Catalog | SketchPad | SketchPad |
| Data Source | DT0105 | DT0105 |
| Auto Translate | TRUE | Yes |
| Use Encryption for Data | FALSE | No |
| | | |
| Use Procedure for Prepare | 1 | |
| Packet Size | 4096 | |
| Workstation ID | DB07 | |
| Tag with column collation when possible | FALSE | |
| Persist Security Info | TRUE | |
| (mode is write) | | |
| | | |
| Data Source Name | | DT0105_SketchPad |
| Data Source Description | | DT0105_SketchPad |
| Language | | (Default) |
| Log Long Running Queries | | No |
| Log Driver Statistics | | No |
| Use Regional Settings | | No |
| Prepared Statements Option | | Drop temporary procedures on disconnect |
| Use Failover Server | | No |
| Use ANSI Quoted Identifiers | | Yes |
| Use ANSI Null, Paddings and Warnings | | Yes |

Not applicable
Author

I have been struggeling with this as well, but I figured it out

I am using sql 2008

1. add mode is write to the back off your connection string:

......] (XPassword is password, mode is write);

or

....] (mode is write)

2. in settings tab -> tick open database in read write mode

3. Here is what changed every thing for me, Create your stored procedure using nocount:

create procedure getdate @Fromdate datetime, @Todate datetime

as

set nocount on

select * from table

set nocount off

go

Not applicable
Author

Thanks for the sharing.

I would be glad to get some help.

1. I got an error when I tried to do 1 step. I cannot add "mode is write" as you discribed.

The error : "Db write mode requires the DB write mode flag to be set on the script!"

2. What is the QV command for calling the stored procedure which you used?

Not applicable
Author

*discribed = described

BTW, I marked the checkbox in the setting tab > user preferences > security >

Script (Allow Database Write and Execute Statements). was it your meaning in step 2 ?