[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

function to return table name

Posted on 2008-10-01
3
Medium Priority
?
825 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 1000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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