﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / Select from Variable Table Name under Function. / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 02:06:52 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Select from Variable Table Name under Function.</title><link>http://www.sqlservercentral.com/Forums/Topic565282-145-1.aspx</link><description>Hi Lawerance,Thank you for your suggestions.I will try to implement by using the stored procedure.Regards,Edward</description><pubDate>Mon, 08 Sep 2008 20:39:21 GMT</pubDate><dc:creator>edward-837480</dc:creator></item><item><title>RE: Select from Variable Table Name under Function.</title><link>http://www.sqlservercentral.com/Forums/Topic565282-145-1.aspx</link><description>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.</description><pubDate>Mon, 08 Sep 2008 08:16:16 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>Select from Variable Table Name under Function.</title><link>http://www.sqlservercentral.com/Forums/Topic565282-145-1.aspx</link><description>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)ASBEGIN  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 @resultEND---------------------------------------------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</description><pubDate>Mon, 08 Sep 2008 04:32:50 GMT</pubDate><dc:creator>edward-837480</dc:creator></item></channel></rss>