Solved

UDF to return 'select * from @tablename'

Posted on 2007-12-03
3
341 Views
Last Modified: 2008-03-03
We have a lot of existing code that refers to table 'Purchases'  Now are getting new clients and want to run the same code but using the new client's purchase tables (Purchases_ad,Purchases_nl etc).

 So I thought a function fn_purchases(whichTable) would do the trick.   The select statement would be  'select * from @tableName.'.  But no go.  I've tried  select @query = 'select * from ' + @tableName. And a variety of other attempts that don't work.

Any hints how this might be done.

thanks,

jim

0
Comment
Question by:studioEtc
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20398301
you cannot use a UDF to run dynamic sql. the only thing you can do is a big IF ELSE IF ... etc to decide which query to run, but then again, the output must be common among all the possible tables.

if that is not possible, you have to use a stored procedure...
0
 

Author Comment

by:studioEtc
ID: 20398395
Thanks!

If I use a stored procedure can I say 'select * from myStoredProcedure(@tableName)'?

Maybe it's better to truncate then repopulate the Purchases Table with data for the client in question before running our code.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20398456
>If I use a stored procedure can I say 'select * from myStoredProcedure(@tableName)'?
unfortunaltey, no.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now