Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

UDF to return 'select * from @tablename'

Posted on 2007-12-03
3
Medium Priority
?
409 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

618 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