You are viewing limited content. For full access, please sign in.

Question

Question

Cant use SQL query in workflow custom query

asked on May 27, 2014

Hello,

I am currently trying to put the following code into a custom query in workflow which will pull down information into fields in a document. My workflow code looks a bit different but the logic is the same. I want to perform a query based on user input. I do not know how to perform this because I get errors when I use if statements. 

 

/* Declare variables to store user input */
DECLARE 
@RefNo VARCHAR ( 25 ) = NULL ,
@Serial VARCHAR ( 25 ) = NULL ,
@CPFile VARCHAR ( 25 ) = NULL
/* ===================================== */

/* Set variables to query */
SET @RefNo =  NULL
SET @Serial = NULL
SET @CPFile = NULL
/* ====================== */


                                         /* Correspondence Logic
   =======================================================================================================
-- If ( User Input (Serial) != NULL ) = WHERE (Serial = User Input)
-- If (User Input (Reference) != NULL ) = WHERE (Reference = User Input)
-- If (User Input (CPFile AND Reference != NULL ) = WHERE (CPFile = User Input AND Reference = User Input )
-- If (User Input (Reference AND Serial != NULL ) = WHERE (Reference = User Input AND Serial = User Input)
-- If (User Input (All) != NULL ) = WHERE (All = User Input)
   ======================================================================================================= */





/* === If user input for the CPFile Number and Reference Number fields are NULL and Serial is populated ===
   ======================================================================================================== */
IF ( ( @CPFile IS NULL ) AND ( @RefNo IS NULL ) AND ( @Serial IS NOT NULL ) )

	SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,										ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName ,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( ATLB.dbo.NOTES.SERIAL = @Serial )
/* =============================================== End =================================================== */
/* ======================================================================================================= */











/*     If user input for the CPFile Number and Serial fields are NULL and Reference Number is populated
   ======================================================================================================= */
ELSE IF ( ( @CPFile IS NULL ) AND ( @Serial IS NULL ) AND ( @RefNo IS NOT NULL ) )

		SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm, ATLB.dbo.PAGE0.ConsumerNm, ATLB.dbo.PAGE0.CPFileNo, ATLB.dbo.PAGE0.defld, ATLB.dbo.PAGE0.RefNo,											ATLB.dbo.PAGE0.Staff, ATLB.dbo.PAGE0.InitType,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE, ATLB.dbo.NOTES.NOTEBY, ATLB.dbo.NOTES.NOTETYPE, ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( RefNo = @RefNo )
/* =============================================== End =================================================== */
/* ======================================================================================================= */











/* === If user input for the CPFile Number is NULL and Reference Number and Serial Number are populated ===
   ======================================================================================================== */
ELSE IF ( ( @CPFile IS NULL ) AND ( @RefNo IS NOT NULL ) AND ( @Serial IS NOT NULL ) )

		SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,										ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName ,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( ( RefNo = @RefNo ) AND ( ATLB.dbo.NOTES.SERIAL = @Serial ) )
/* =============================================== End =================================================== */
/* ======================================================================================================= */










/*	============================ If user input for all fields is populated ===============================
   ======================================================================================================= */
ELSE
		SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,									    ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName ,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( ( RefNo = @RefNo ) AND ( ATLB.dbo.NOTES.SERIAL = @Serial ) AND ( CPFileNo = @CPFile ) )
/* =============================================== End =================================================== */
/* ======================================================================================================= */
 

Thanks for any help!

0 0

Answer

APPROVED ANSWER
replied on June 24, 2014

Just for reference, the issue was the null token formatting. When it says to apply null token formatting expression, all you need to do is type NULL for the formatting and it should use null if the field is blank. 

0 0

Replies

replied on May 27, 2014

What does the error message say?

0 0
replied on May 27, 2014

Well theres a few, one is an error converting null to int but I fixed that. Then I just get "There was an error near )" or "There was an error near AND". 

0 0
replied on May 27, 2014 Show version history

Does it successfuly execute in a SQL Studio query?

0 0
replied on May 27, 2014

Yes it does.

0 0
replied on May 27, 2014

I guess my main question is why my if statements are not correct in workflow. They work fine in sql studio.

0 0
replied on May 27, 2014

Workflow just passes the parameter data and the query to the SQL driver, it doesn't do any validation or parsing on the query. So that error comes from the driver. Given that you're naming your parameters, I take you're using the "direct" option for the data source?

0 0
replied on May 27, 2014

I have 3 parameters set which the user enters then I query based off these values using the if statements. I had the values set to NULL in studio but have them changed to be a different value as the null was causing problems.

0 0
replied on May 27, 2014

I was looking around and found Advanced Search Syntax 8.2 pdf so I tried my code like this in workflow:

 

IF ( @CPFile <> "99-9999" & @Serial ="99999" & @RefNo = "99999999")
    	SELECT
		PAGE0.CompanyNm , 
		PAGE0.ConsumerNm , 
		PAGE0.CPFileNo , 
		PAGE0.defld , 
		PAGE0.RefNo , 
		PAGE0.Staff , 
		PAGE0.InitType , 
		Typelaw.TypeName , 
		NOTES.DUEDATE , 
		NOTES.NOTEBY , 
		NOTES.NOTETYPE , 
		NOTES.SERIAL

FROM PAGE0
  
INNER JOIN Typelaw
ON ( PAGE0.TYPELAW = Typelaw.Serial )
  
INNER JOIN NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )
WHERE (  CPFileNo = @CPFile)

@CPFile, @RefNo, and @Serial are the three parameters also.
Any ideas what I should do?

1 0
replied on May 28, 2014

Hi Josh,

 

You could avoid the problem by putting your SQL code into a stored procedure and then just calling that from Workflow. Given stored procedures are pre-compiled by the sql server, it may even execute measurably faster.

 

http://msdn.microsoft.com/en-us/library/dn133184.aspx

 

-Ben

0 0
replied on May 28, 2014

Thank you, here is what I've got so far:

 


-- =============================================
-- Author:		Josh Schweigert
-- Create date: 05/28/2014
-- Description:	Stored procedure of the combined query for workflow
-- ===========================================

CREATE PROCEDURE Consumer_Stored_Procedure_2
(
	 @CPFile varchar (25),
	 @RefNo varchar (25),
	 @Serial varchar (25)


)

AS 
BEGIN

                                         /* Correspondence Logic
   =======================================================================================================
-- If ( User Input (Serial) != NULL ) = WHERE (Serial = User Input)
-- If (User Input (Reference) != NULL ) = WHERE (Reference = User Input)
-- If (User Input (CPFile AND Reference != NULL ) = WHERE (CPFile = User Input AND Reference = User Input )
-- If (User Input (Reference AND Serial != NULL ) = WHERE (Reference = User Input AND Serial = User Input)
-- If (User Input (All) != NULL ) = WHERE (All = User Input)
   ======================================================================================================= */





/* === If user input for the CPFile Number and Reference Number fields are NULL and Serial is populated ===
   ======================================================================================================== */
IF ( ( @CPFile IS NULL ) AND ( @RefNo IS NULL ) AND ( @Serial IS NOT NULL ) )

	SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,										ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName ,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( ATLB.dbo.NOTES.SERIAL = @Serial )
/* =============================================== End =================================================== */
/* ======================================================================================================= */











/*     If user input for the CPFile Number and Serial fields are NULL and Reference Number is populated
   ======================================================================================================= */
ELSE IF ( ( @CPFile IS NULL ) AND ( @Serial IS NULL ) AND ( @RefNo IS NOT NULL ) )

		SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm, ATLB.dbo.PAGE0.ConsumerNm, ATLB.dbo.PAGE0.CPFileNo, ATLB.dbo.PAGE0.defld, ATLB.dbo.PAGE0.RefNo,											ATLB.dbo.PAGE0.Staff, ATLB.dbo.PAGE0.InitType,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE, ATLB.dbo.NOTES.NOTEBY, ATLB.dbo.NOTES.NOTETYPE, ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( RefNo = @RefNo )
/* =============================================== End =================================================== */
/* ======================================================================================================= */











/* === If user input for the CPFile Number is NULL and Reference Number and Serial Number are populated ===
   ======================================================================================================== */
ELSE IF ( ( @CPFile IS NULL ) AND ( @RefNo IS NOT NULL ) AND ( @Serial IS NOT NULL ) )

		SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,										ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName ,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( ( RefNo = @RefNo ) AND ( ATLB.dbo.NOTES.SERIAL = @Serial ) )
/* =============================================== End =================================================== */
/* ======================================================================================================= */










/*	============================ If user input for all fields is populated ===============================
   ======================================================================================================= */
ELSE
		SELECT

	-- From PAGE0 Table
		ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,									    ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,

	-- From TypeLaw Table
		ATLB.dbo.Typelaw.TypeName ,

	-- From NOTES Table
		ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL

FROM [ATLB].[dbo].[PAGE0]
  
INNER JOIN ATLB.dbo.Typelaw
ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
  
INNER JOIN ATLB.dbo.NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )

WHERE ( ( RefNo = @RefNo ) AND ( ATLB.dbo.NOTES.SERIAL = @Serial ) AND ( CPFileNo = @CPFile ) )
/* =============================================== End =================================================== */
/* ======================================================================================================= */

END

 

0 0
replied on May 28, 2014

Is this the correct way to do this? In workflow I have it call the procedure using:

 

exec "procedure" @Input_parameter1...

 

But I still dont get results.

0 0
replied on May 28, 2014

Scratch that - now I have a closer look at your code (I jumped the gun earlier) It looks like a solution would be to create a set of individual views and have a decision branch activity decide which one to select from. 

 

 

0 0
replied on May 28, 2014 Show version history

a stored procedures would need output variables defined but at any rate, I think creating views would be more appropriate to your requirement.

0 0
replied on May 28, 2014

So what exactly do you mean by "views" like case statements?

0 0
replied on May 28, 2014

 

030

Create View CaseInfo

as

031  
032     SELECT
033  
034     -- From PAGE0 Table
035         ATLB.dbo.PAGE0.CompanyNm , ATLB.dbo.PAGE0.ConsumerNm , ATLB.dbo.PAGE0.CPFileNo , ATLB.dbo.PAGE0.defld , ATLB.dbo.PAGE0.RefNo ,                                      ATLB.dbo.PAGE0.Staff , ATLB.dbo.PAGE0.InitType ,
036  
037     -- From TypeLaw Table
038         ATLB.dbo.Typelaw.TypeName ,
039  
040     -- From NOTES Table
041         ATLB.dbo.NOTES.DUEDATE , ATLB.dbo.NOTES.NOTEBY , ATLB.dbo.NOTES.NOTETYPE , ATLB.dbo.NOTES.SERIAL
042  
043 FROM [ATLB].[dbo].[PAGE0]
044    
045 INNER JOIN ATLB.dbo.Typelaw
046 ON ( PAGE0.TYPELAW = ATLB.dbo.Typelaw.Serial )
047    
048 INNER JOIN ATLB.dbo.NOTES
049 ON ( PAGE0.SERIAL = NOTES.CaseSerial )
050  
051 go

 

please keep in mind that I'm not recommending views. t's just a suggestion based on little knowledge of your requirements. I like to work with views though.

 

then you can use like a regular table;

select * from CaseInfo where serial='3323'

 

 

0 0
replied on May 28, 2014

So it allows you to just do a single query by using a select statement where a field has a specific value? How would I use IF cases in the workflow query editor to use this on specific cases of user input?

 

Thank you for all your help thus far!

0 0
replied on May 28, 2014

Here is what I did and I get the following error:

(If the user input for any of the three main variables is blank it is set to a default value. CPFile = 99-9999, Serial = 99999, and RefNo = 99999999

CREATE VIEW QueryView2 AS

SELECT
PAGE0.CompanyNm , 
PAGE0.ConsumerNm ,
PAGE0.CPFileNo ,
PAGE0.defld ,
PAGE0.RefNo ,										PAGE0.Staff ,
ATLB.dbo.PAGE0.InitType ,
Typelaw.TypeName ,
NOTES.DUEDATE , 
NOTES.NOTEBY , 
NOTES.NOTETYPE , 
NOTES.SERIAL

FROM PAGE0
  
INNER JOIN Typelaw
ON ( PAGE0.TYPELAW = Typelaw.Serial )
  
INNER JOIN NOTES
ON ( PAGE0.SERIAL = NOTES.CaseSerial )
go

IF ( @CPFile = '99-9999' AND @RefNo = '99999999' AND @Serial <> '99999' )
     SELECT * FROM QueryView2 WHERE NOTES.SERIAL = @Serial

0 0
replied on May 29, 2014 Show version history

Hi you can use WF decision branches to decide how to query the view.

Your IF statement contains non-declared variables and shouldn't be run the same time as you crate your view (not sure if that's causing the error but it can be problematic). I think you should read an introductory SQL book over the weekend ;). I'm really not in a position to give you advice, just point you in a direction and hope it's the right one! 

 

 

0 0
replied on May 29, 2014

Well the decision branches are how its set up now. I was trying to convert all of them into a single query rather than 4 separate branches. Do you have any recommendations of books to read? I've just been winging it and learning off google!

 

Thanks.

0 0
replied on May 29, 2014
The risk of putting everything into SQL is limiting the number of people who can maintain the workflow if you go on holiday or leave the company or need help from laserfiche. Even though spreading the logic between SQL, workflow and query activities does require more interdependencies and documentation... Just my 2c though. The Dummies series is very good but I've not read a publication in years. Google is start I reference mostly these days.
0 0
replied on May 29, 2014

I understand, but I'm not the only person who works on this so if either of us make changes we explain it to each other so we both understand how it works. They just wanted to find out if it was possible to convert everything into a single query rather than separate queries. I guess I dont understand what the problem is with taking code written in sql studio and bringing it into workflow. But it just gives errors.

0 0
replied on June 3, 2014

Tip: If you want a token to be replaced by the null value in the case that the token's value is blank (the empty string), apply the NULLtoken formatting expression. If you want to set a column's value to the null value regardless, insert the NULL token: %(DB.NULL).

 

How do I set the parameter to null if the user doesnt enter anything?

0 0
APPROVED ANSWER
replied on June 24, 2014

Just for reference, the issue was the null token formatting. When it says to apply null token formatting expression, all you need to do is type NULL for the formatting and it should use null if the field is blank. 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.