473,402 Members | 2,061 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,402 software developers and data experts.

User Defined Function: Convert String value of Table to Table Object

Does anyone know where to find or how to write a quick user defined fucntion
that will return a table object when passed the string name of the table
object. The reason why I want dynamicallly set the table name in a stored
procudue WITHOUT using concatination and exec a SQL String.

Hence

If @small_int_parameter_previous = 1 then
@vchar_tablename = "sales_previous"
else
@vchar_tablename = "sales"
End

select * from udf_TableLookup(@vchar_tablename )

So if I pass 1, that means I want all records from "sales_previous"
otherwise give me all records from "sales" (Sales_Previous would last years
sales data for example).

udf_TableLookup would I guess lookup in sysobjects for the table name and
return the table object? I don't know how to do this.

I want to do this to avoid having 2 stored procedures..one for current and
one for previous year.

Please respond to group so others may benfiit from you knowledge.

Thanks

Erik
Jul 20 '05 #1
2 3356
On Tue, 17 Aug 2004 08:51:44 -0700, Erik Grob (MCP) wrote:
Does anyone know where to find or how to write a quick user defined fucntion
that will return a table object when passed the string name of the table
object. The reason why I want dynamicallly set the table name in a stored
procudue WITHOUT using concatination and exec a SQL String.

Hence

If @small_int_parameter_previous = 1 then
@vchar_tablename = "sales_previous"
else
@vchar_tablename = "sales"
End

select * from udf_TableLookup(@vchar_tablename )

So if I pass 1, that means I want all records from "sales_previous"
otherwise give me all records from "sales" (Sales_Previous would last years
sales data for example).

udf_TableLookup would I guess lookup in sysobjects for the table name and
return the table object? I don't know how to do this.

I want to do this to avoid having 2 stored procedures..one for current and
one for previous year.

Please respond to group so others may benfiit from you knowledge.

Thanks

Erik


Others may know better than I, but I don't think you can make this
completely general.

The table structure returned by your UDF has to be the same no matter which
table gets selected from. So right there, you have a limitation - you
can't return *ANY* table.

Then, there's no "table object" that can be returned. You have to use a
select, no matter what you do.

You could use a construct like this:

create function udf_TableLookup( @tablename varchar(20) )
returns @retTable table (
salesDate datetime,
salesman varchar(10),
saleAmount money(6,2)
)
AS
BEGIN
IF @tablename = 'sales' then
INSERT @retTable
SELECT * from SALES
ELSE
IF @tablename = 'sales_previous' then
INSERT @retTable
SELECT * from SALES_PREVIOUS
ELSE
IF @tablename = 'sales_ancient' then
INSERT @retTable
SELECT * from SALES_ANCIENT
END

That would free you from multiple stored procedures...
Jul 20 '05 #2
Thanks! You are correct, I have this user defined function that is great
and I thought I could do something with:
CREATE FUNCTION fnSplitIntoTable (@InString VARCHAR(4000), @DELIMITER
CHAR(1))
RETURNS @OutTable TABLE (ValuesList VARCHAR(255))
AS
BEGIN
DECLARE @pos INT
SET @InString = RTRIM(LTRIM(@InString))
SET @pos = CHARINDEX(@DELIMITER,@InString,1)
WHILE @pos <> 0
BEGIN
INSERT INTO @OutTable
SELECT LTRIM(RTRIM(LEFT(@InString,@pos-1)))
SET @InString = SUBSTRING(@InString,@pos+1,LEN(@InString))
SET @pos = CHARINDEX(@DELIMITER,@InString,1)
END
INSERT INTO @OutTable
SELECT @InString
RETURN
END
but you're right when you return a TABLE you have to give the structure I
guess, so the udFunction would actually have to popuate and ENTIRE (temp)
table and return it... YUCK!!!!

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
13
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.