If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Please help in SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 05-17-04, 13:22
Enigma Enigma is offline
The SQL Apostle
 
Join Date: Jul 2003
Location: The Dark Planet
Posts: 1,394
Well ... am running busy ... will try to work on the function tomorrow ... will that do ??
__________________
Get yourself a copy of the The Holy Book

order has no physical Brett in The meaning of a Kaiser . -database data
Reply With Quote
  #17 (permalink)  
Old 05-17-04, 13:28
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
will, seems far away from what I want.

How to return the table-valued function?
Reply With Quote
  #18 (permalink)  
Old 05-17-04, 13:39
TallCowboy0614 TallCowboy0614 is offline
Throwin' the steel to SQL
 
Join Date: Feb 2004
Location: Marina Del Rey, CA
Posts: 3,667
I will also take a stab at this later this morning, but you may in the meantime want to look at books online (if you have access to it), or do an online search, for user defined functions - they can return tables or scalar values.

It actually seems just what you want, IMHO, if what you want returned is data that you will be writing into a new table (each of your call segments) or you can use the table returned from the function in another query or a cursor of it's own if you are doing something with the segments other than simply writing them out to a table after you create them.

Like I said, I'll take a stab later this morning, as I have not built a table from scratch in my table-returning functions, but have many that return a table built with a select statment instead (not what you want).
__________________
aka "Paul"
Non est ei similis.

I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them
Reply With Quote
  #19 (permalink)  
Old 05-17-04, 13:41
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
Thanks guys.

I am doing research now. Books and on-line.

Tomorrow is ok for me

Thanks again.
Reply With Quote
  #20 (permalink)  
Old 05-17-04, 15:56
TallCowboy0614 TallCowboy0614 is offline
Throwin' the steel to SQL
 
Join Date: Feb 2004
Location: Marina Del Rey, CA
Posts: 3,667
There is probably an easier (AKA fancier) way to accomplish this,

but I tested a few numbers, and think this works the way you want things to work.

It does return a table of rows formatted the way I think you want to insert them...so theoretically, you could just call the function inline like:
Code:
INSERT FinalTable dbo.getInd(@TotalMins)
or, perhaps more in line with your purposes, the results returned by the function can be used as any "standard" table (I.e., "SELECT * from dbo.getInd" or any variation thereof).

Code:
CREATE FUNCTION getInd(@CT int) 
RETURNS @CallSegments TABLE	(segno int, 
				 timeslice int, 
				 indicator int)
AS
BEGIN

DECLARE @SegNum int
SET @SegNum = 1

IF (@CT < 200)
	BEGIN
		INSERT @CallSegments VALUES (@SegNum, @CT, (@CT / 100))
		SET @SegNum = @SegNum + 1
	END
ELSE
	WHILE (@CT > 0)
		BEGIN
			IF ((@CT >= 200) AND (@SegNum = 1))
				INSERT @CallSegments VALUES (@SegNum, 200, 4)
			ELSE 	IF (@CT < 200)
					INSERT @CallSegments VALUES (@SegNum, @CT, 6)
				ELSE
					BEGIN
						INSERT @CallSegments VALUES(@SegNum, 100, 5)
						SET @SegNum = @SegNum + 1
						INSERT @CallSegments VALUES(@SegNum, 100, 5)
					END
			SET @SegNum = @SegNum + 1
			SET @CT = @CT - 200
		END
		
RETURN
END
Hopefully this, or a variation of it, can get you where you need to go.
__________________
aka "Paul"
Non est ei similis.

I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

Last edited by TallCowboy0614; 05-17-04 at 15:58.
Reply With Quote
  #21 (permalink)  
Old 05-18-04, 09:56
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
Thank you very much. It helps alot.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On