Solved

User Defined Functions in SQL statements

Posted on 1998-08-05
5
436 Views
Last Modified: 2010-03-19
I want to know whether a user defined functions can be used inside a SQL statement.  Suggest a solution as how to give a user defined function inside a SQL statement.

0
Comment
Question by:vvg
5 Comments
 
LVL 5

Expert Comment

by:mayhew
Comment Utility
When you say 'user defined function' do you mean a stored procedure?

If I'm understanding your question, you cannot call a stored proc from inside a SQL statement.

select * from (exec (sp_stored_proc))

or

select * from <table> where <name> in (exec(stored_proc))

or any other variation will not work.

The idea is to put everything you will be needing into your stored proc and then calling it by itself.

Now what you *can* do is to call a stored proc from within a stored proc.  Declare variables in your first proc that you pass to your second proc.  In the second proc, make those parameters OUTPUT params.

Example

create procedure sp1 as
declare
  @var1 int,
  @var2 int
begin

select @var1 = id1 from table1
select @var2 = id2 from table2

exec sp2 @var1, @var2

select * from mytable where id1 = @var1 and id2 = @var2

end


create procedure sp2 @outvar1 int OUTPUT, @outvar2 int OUTPUT as
begin

if @outvar1 < @outvar2 then select @outvar2 = 10    --or whatever.

end


I hope this is helpful!  :)

Don
0
 

Author Comment

by:vvg
Comment Utility
Hi! Don, I think u haven't understood my question, also I'm sorry for not giving a detailed question. Here's my requirement,

I have an SQL in Access which is like this:

Select * from table1 as a, table1 as b where
(timespan(a.date1,a.date2,b.date1,b.date2) = true) and ....

Here timespan is a user-defined function in Access which accepts 4 dates are parameters and does some checking and returns true or false.

In SQL Server we can create a stored proc for this which will return true or false but the problem will be executing this stored proc inside the SQL ( ie using it in the Where clause).

So, how to go about it.

Thanks for u'r suggestion and hopefully if u could give the solution for the above specified problem, it will be of great help to  me.

Regards
vvg.


0
 

Expert Comment

by:zimmy
Comment Utility
It would help if you gave an example of what the timespan function does . . .

One possible solution would be to write timespan as a separate stored procedure, call it from the first (with parameters if needed) and check the return code. This is what mayhew suggested. I know it's not quite the same as a function call, but it could be close enough, depending . . .

Zimmy
0
 
LVL 4

Accepted Solution

by:
mitek earned 30 total points
Comment Utility
There seems to be no way (i know of) to call a stored procedure from within a SELECT query in MS SQL Server (in Oracle, it IS possible though)

When I had to do something similar, I used code like the one below. Not pretty, but worked. Of course, it would be best to get rid of the function and try to figure out a way to do the timespan check in SQL query w/o SP. But if you ABSOLUTELY need to use an SP (like, if the procedure is extremely complex), take a look at the code below ...

DECLARE @date1a datetime,@date2a datetime,@date1b datetime,@date2b datetime -- + other vars needed
DECLARE @status smallint -- return statuus for SP

CREATE TABLE #temp(date1a datetime,
                   date2a datetime,
                   date1b datetime,
                   date2b datetime) -- + other columns if needed

DECLARE csr CURSOR FOR
  SELECT a.date1,a.date2,b.date1,b.date2 -- + other columns needed
    FROM table1 a, table2 b
   WHERE a.id = b.id FOR READ ONLY -- whatever join condition is required
OPEN csr

WHILE @@FETCH_STATUS <> -1 BEGIN
  FETCH NEXT FROM csr INTO @date1a,@date2a,@date1b,@date2b -- + other variables corresp. to columns
  EXEC @status = timespan @date1a,@date2a,@date1b,@date2b

  IF @status = 1 BEGIN
    INSERT INTO #temp(date1a,date2a,date1b,date2b) -- + other column names
    VALUES (@date1a,@date2a,@date1b,@date2b)       -- + other data
  END

END

CLOSE csr
DEALLOCATE csr

SELECT * FROM #temp

-- temporary table #temp will be dropped automatically when sp finishes

0
 
LVL 5

Expert Comment

by:mayhew
Comment Utility
wg,

There is no way to call a stored proc from within a where clause.  What mitek is suggesting is pretty much what I suggested except that he is using the return code from the stored proc instead of variables that you pass to the stored proc.  Either should work.  It's up to you if want to experience the joy of using a cursor.  :)

I think the best solution for you would be to include your select in the stored proc that figures timspan as I suggested in my previous post.

That way instead of saying:
select * from table where exec sp_stored_proc

you would make your select the last line of your stored proc and then just call:
exec sp_stored_proc

If you're having trouble with this at all, feel free to post further questions (of course) or you can e-mail me direct if you choose.

don@mayhewnet.com

Let me know if this works for you.  :)

Don
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now