Solved

function to return table name

Posted on 2008-10-01
3
816 Views
Last Modified: 2012-08-14
From a stored procedure, can I call a custom function that will return the table name I want to use in my SQL syntax?

thanks in advance,
Bill
ALTER FUNCTION [GetTableName](@Criteria VARCHAR(1))
RETURNS varchar(50)
AS
BEGIN
DECLARE @Output varchar(50)
SET @Output = ''
 
SELECT @Output = 
CASE WHEN @Criteria = 'A' THEN 'TABLE_A'
WHEN @Criteria = 'B' THEN 'TABLE_B'
WHEN @Criteria = 'C' THEN 'TABLE_C'
END
 
RETURN @Output
END
 
 
 
Create PROCEDURE [dbo].[sp_TestStoreProc]
 SELECT TOP 1 * FROM + GetTableName('B') 
END

Open in new window

0
Comment
Question by:woyler
[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 60

Expert Comment

by:chapmandew
ID: 22618645
have to change your proc around bit:

Create PROCEDURE [dbo].[sp_TestStoreProc]
BEGIN
declare @x nvarchar(3000)
set @x = ' SELECT TOP 1 * FROM '+ GetTableName('B')
exec sp_executesql @x
END
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 22618653
also, have to include dbo. on your function call

Create PROCEDURE [dbo].[sp_TestStoreProc]
BEGIN
declare @x nvarchar(3000)
set @x = ' SELECT TOP 1 * FROM '+ dbo.GetTableName('B')
exec sp_executesql @x
END
0
 

Author Closing Comment

by:woyler
ID: 31502150
Perfect.  Thank you for the quick response.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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