?
Solved

UDF to return 'select * from @tablename'

Posted on 2007-12-03
3
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 143

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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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
Suggested Courses

777 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