1

Is it possible to use DB name as parameter in user-defined function?

For example I've tried to use concatenation but it doesn't work in this way:

create function fn_getEntityOwners (@dbName varchar, @entityId bigint)
returns table
as
return 
select
    OWNERS.USER_ID 
from 
    @dbName + '.dbo.OWNERS' as OWNERS
where 
    OWNERS.ENTITY_ID = @entityId

UPDATE: I use sql server

4
  • possible duplicate of How to use variable for database name in t-sql Commented Jan 16, 2014 at 16:37
  • @AndriyM My question is more specific because I need to use DB name as parameter in user-defined function and function imposes some restrictions. Commented Jan 16, 2014 at 16:51
  • 1
    Well, you just can't parametrise a name (DB name, column name, table name). Almost all the answers (both there and here) suggest using dynamic SQL. You can't use dynamic SQL in a function, which means you are getting nowhere with this particular question. I would suggest you ask a new question, where you might want to elaborate a little on what you are trying to do, and where you would ask specifically for alternatives to using dynamic SQL (or, perhaps, for alternatives to using a function?). Commented Jan 16, 2014 at 17:04
  • Thank you for your suggestion, I've added a new question link Commented Jan 16, 2014 at 17:49

3 Answers 3

3

Put your code in function into a string and execute by dynamic sql

DECLARE @SQL varchar(2000)

SELECT @SQL = 

'select
    OWNERS.USER_ID 
from 
    ' + @dbName + '.dbo.OWNERS as OWNERS
where 
    OWNERS.ENTITY_ID = ' + @entityId


EXEC(@SQL)
Sign up to request clarification or add additional context in comments.

4 Comments

Don't like this approach much because query becomes a string and isn't validated by SQL Server Management Studio. But anyway thank you for your solution. Other ideas?
are you worried about the database not existing and throwing an error? if so can always add code to check if exists before querying for the user_id...IF db_id(@dbName) IS NOT NULL BEGIN...
No, I'm worried about situation when you make mistake in sql query but because it is just a string you will know this only after function execution. I've shown just a simple example but imagine that query inside function is big and complicated.
It is not possible to use exec in function
1

You need to use dynamic SQL to do this. What DB is this? Here's another SO question that relates

2 Comments

Great, SQL Server will support dynamic SQL.
See my comment to @DominionDave
0

You can also use sqlcmd mode for this (enable this on the "Query" menu in Management Studio).

:setvar dbname "TEST.dbo.OWNERS" 

select
    OWNERS.USER_ID 
from 
    $(dbname) as OWNERS
where 
    OWNERS.ENTITY_ID = @entityId


... this solution brought to you by Stack Overflow ...


whatup @DominionDave - Nice to have you on SO playa :-D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.