473,671 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_para meter_previous = 1 then
@vchar_tablenam e = "sales_previous "
else
@vchar_tablenam e = "sales"
End

select * from udf_TableLookup (@vchar_tablena me )

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 3364
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_para meter_previous = 1 then
@vchar_tablenam e = "sales_previous "
else
@vchar_tablenam e = "sales"
End

select * from udf_TableLookup (@vchar_tablena me )

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 fnSplitIntoTabl e (@InString VARCHAR(4000), @DELIMITER
CHAR(1))
RETURNS @OutTable TABLE (ValuesList VARCHAR(255))
AS
BEGIN
DECLARE @pos INT
SET @InString = RTRIM(LTRIM(@In String))
SET @pos = CHARINDEX(@DELI MITER,@InString ,1)
WHILE @pos <> 0
BEGIN
INSERT INTO @OutTable
SELECT LTRIM(RTRIM(LEF T(@InString,@po s-1)))
SET @InString = SUBSTRING(@InSt ring,@pos+1,LEN (@InString))
SET @pos = CHARINDEX(@DELI MITER,@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
2742
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 = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
13
3485
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 the function that i want to write. The student table is:- roll name 1 'A'
4
2510
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 Inherits System.Windows.Forms.UserControl Private _Collection As GridTableStylesCollection
6
7607
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, Karthi
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.