473,382 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Use output of Stored Procedure as where condition

31
I have one stored procedure, which defines where condition based useing Case when...I m storing this Condition in one outpur variable of Store procedure.


I would like to now use this condition in view or Query...

Pls guide how to do this or is there any other way of doing the same?

Also note that, my case when defines entire condition .. It is not only giving criteria... like = or > part..

It is giving everthing after where...

Can anyone guide me...

Thanks
Aug 8 '08 #1
10 4437
debasisdas
8,127 Expert 4TB
kindly post the code that you are working on for reference of our experts.
Aug 8 '08 #2
BHTanna
31
Pls find my process..

I m having one master table where i m having Customer id and its bill generation date (called BC). Another table is having customer id, payment amount and payment date(field name Pdate).

My requirement is to find, payment amount customer wise, but for closed BC. BC Closed exactly after one month. And that too, till date. I mean my till date is from BC to the day of today but that day will fall between BC and its closure..
That is called Last Month Same Date. (LMSD).

To do so, i m defining where condition which will be opt dynamically. for that i have written following process.


Create proc lmsd
@bc smalldatetime,
@con varchar(500) output
as

set @con = case when day(getdate()-1) > day(@bc) then
'day(pdate) between day(b.bc) and day(getdate()-1)'
else 'day(pate) between day(b.bc) and [dbo].[ufn_GetDaysInMonth](b.bc)
or day(pdate) between 1 and day(getdate()-1)' end


Now i want use this condition in where clause of my query / view.

Pls help.. Its bit URGENT.
Aug 9 '08 #3
ck9663
2,878 Expert 2GB
Convert it to function so that you can embed it on your query.

-- CK
Aug 9 '08 #4
BHTanna
31
Thanks...

Its working but now only problem i m facing in query and not able to resolve.

Pls help..

function

Create FUNCTION cont ( @bc DATETIME,@gt datetime )
RETURNS varchar(500)
AS
BEGIN

RETURN
case when day(@gt-1) > day(@bc) then
'day(coldate) between day(b.bc) and day(getdate()-1)'
else 'day(coldate) between day(b.bc) and [dbo].[ufn_GetDaysInMonth](b.bc) or day(coldate) between 1 and day(getdate()-1)'
end


END

Query


select bm.bc,max(coldate)
from gujsapcollection g inner join bcwise b on g.caf=b.caf
inner join bcmaster bm on bm.bc=b.bc
where coldate between b.bc and closedt
and [dbo].[cont](b.bc,getdate())

I m getting an error...

Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near ')'.
Aug 11 '08 #5
ck9663
2,878 Expert 2GB
Your WHERE condition is incomplete:


Select bm.bc,max(coldate)
from gujsapcollection g inner join bcwise b on g.caf=b.caf
inner join bcmaster bm on bm.bc=b.bc
where coldate between b.bc and closedt
and [dbo].[cont](b.bc,getdate())


The highlighted part must be a boolean condition.

-- CK
Aug 11 '08 #6
BHTanna
31
But, this user defined function returns condition which will have boolean output...

This is the basic requirement to use dynamic where part...
Aug 12 '08 #7
ck9663
2,878 Expert 2GB
Nope. Your function is returning a string

"RETURNS varchar(500)"

-- CK
Aug 12 '08 #8
BHTanna
31
But that varchar string will be parse here in query no???

Wont the function will be replaced with its output string and entire query will be executed then after ???

Is there any way to check what exactly function in returning???
Aug 12 '08 #9
ck9663
2,878 Expert 2GB
Question:

your function basically says:

Expand|Select|Wrap|Line Numbers
  1. case 
  2.    when day(@gt-1) > day(@bc) then
  3.       'day(coldate) between day(b.bc) and day(getdate()-1)'
  4.    else 
  5.       'day(coldate) between day(b.bc) and [dbo].[ufn_GetDaysInMonth](b.bc) or day(coldate) between 1 and day(getdate()-1)'
  6. end
if row 1 falls on WHEN part and record 2 falls on the ELSE part, will the two record be returned?

Is your condition one or the other? If it is, you might just use a UNION as a subquery.

-- CK
Aug 12 '08 #10
BHTanna
31
Let me tell you, what my function is doing exactly...

Here, getdate() is 13-Aug....

If BC date is 18-June i want payment made between 18-June and 12-July..
If BC date is 27-June i want payment made between 27-June and 12-July..
If BC date is 1-July i want payment made between 1-July and 12-July..
If BC date is 3-July i want payment made between 3-July and 12-July..
If BC date is 5-July i want payment made between 5-July and 12-July..
If BC date is 11-July i want payment made between 11-July and 12-July..

So, in first 2 cases, i need to take payment between 18-Jun and 30-Jun and 1-July to 12-July... That is my else conidtion

but for rest 4 cases, my first part of case when will work...

Pls guide me how should i adjust this condition in query...

Thanks 4 your kind support...
Aug 13 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Robert Scheer | last post by:
Hi. I have a stored procedure on a Oracle 8.1.6 database that generates a dynamic sql statement. This stored procedure has an output parameter that needs to return a count from a view. I can...
2
by: Begoņa | last post by:
in my java application I've made a call to this stored procedure CREATE procedure pruebaICM @pANI varchar(20), @pTABLA varchar(20), @pInsert varchar(500), @pUpdate varchar(1000), @pFLAG...
2
by: Rhino | last post by:
Is there some kind of requirement that a DECLARE CONTINUE HANDLER statement follow the DECLARE CURSOR statement in an SQL stored procedure? I am running DB2 V8.2.1 on Windows XP and am writing my...
4
by: Mr Not So Know It All | last post by:
im new to SQL Server and ASP.Net. Here's my problem. I have this SQL Server stored procedure with an input parameter and output parameter CREATE PROCEDURE . @in_rc varchar(8) @out_eList...
4
by: lokesh.kris | last post by:
I'm having a tough time trying to run this stored procedure in DB2 UDB 8.1 environment. Can someone help me out here. All I'm trying to do here is update an indicator 'N' if it is a NULL....
1
by: jason | last post by:
I've got this really simple table with two fields book: name varchar(50) phone varchar(50) and simple stored procedure jcp1: ALTER procedure "jcp1" @name varchar(50) AS
3
by: bobc | last post by:
Using SQL Server 2000... I wrote a wrapper to call a sub proc (code provided below). The intended varchar value returned in the output parameter of each proc is a string implementation of an...
1
by: BHTanna | last post by:
I have one stored procedure, which defines where condition based useing Case when...I m storing this Condition in one outpur variable of Store procedure. I would like to now use this condition...
0
by: a573851 | last post by:
I have a very lengthy stored procedure that I just inherited. Apparently this stored proc performs poorly. I am reviewing it to see where I can add some efficiencies. I would like to know if there...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.