SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select from Variable Table Name under Function.


Select from Variable Table Name under Function.

Author
Message
edward-837480
edward-837480
SSC-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 119
Dear Experts,

I need to write the following Funtion to output the TimeStr from a TimeCard table as follows:
----------------------------------------------------

CREATE FUNCTION _fGetTimeStr (@ICard int, @DayFile char(8))
RETURNS varchar(100)
AS
BEGIN
declare @tHHMI char(5) -- In format of hh:mi eg. 07:30 or 13:45
declare @result varchar(100) -- Expect output result '07:30,13:45'
SET @tHHMI = ''
SET @result = '--'

-- However the @DayFile is a variable in format: _Xyymmdd eg. _X080901 (sep 1 2008)

DECLARE fil CURSOR FOR

SELECT Xtime FROM _X080901
WHERE ICard = @ICard
ORDER by Xtime

-- Alright when writing the exact table name,
-- but get error when _X080901 is replaced by @DayFile

OPEN fil

FETCH NEXT FROM fil INTO @tHHMI

WHILE @@fetch_status = 0
BEGIN
IF @result = '--'
BEGIN
SET @result = @tHHMI
END
ELSE
BEGIN
SET @result = @result +','+ @tHHMI
END

FETCH NEXT FROM fil INTO @tHHMI
END
CLOSE fil
DEALLOCATE fil
return @result
END
---------------------------------------------

Can any expert suggest me how to do ?

Is there any alternate way to achieve my requirement ?

Thanks a lot for your help !


Regards,
Edward
GSquared
GSquared
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93729 Visits: 9730
The only way to have a variable table name would be to use dynamic SQL. You can't use dynamic SQL in a function.

Can you use a proc instead of a function and have the return value changed to an output parameter?

Alternately, you might be able to create a view that selects the required data from all of the tables, and have the function select from the view. If you regularly add tables (which is a really, really bad idea), you could set up a job to alter the view as needed.

A better idea is to change the table structure so that the date is a column instead of part of the table name. That will prevent all kinds of problems.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
edward-837480
edward-837480
SSC-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 119
Hi Lawerance,

Thank you for your suggestions.

I will try to implement by using the stored procedure.

Regards,
Edward
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search