Solved

UDF to return 'select * from @tablename'

Posted on 2007-12-03
3
363 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL with ODBC 5 35
Simple SQL query from two tables 13 53
TSQL query to generate xml 4 32
Help in Bulk Insert 9 30
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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