D
D
damne12iche2019-09-07 18:19:17
Transact SQL
damne12iche, 2019-09-07 18:19:17

How to use other databases in a function?

There is a simple function that returns a table - SELECTvalues ​​by identifier, for example

CREATE FUNCTION dbo.func1 (@dbase varchar(max),@t1f1var int)
RETURNS @restbl
TABLE(t1f1 int,t1f2 int) 
AS
BEGIN
    insert into @restbl
  select t1f1, t1f2
  from (хочу сюда писать '[email protected]+')dbo.t1
  where t1f1= @t1f1var
  return;
END

In fact, this table is in all my 20 databases, I want to access any of them from one and get the value. But I can’t assign the second var parameter there, which will allow me to select a database
(by type dbname1.dbo.t1), please tell me, how to do it, and even better if you attach an informative resource on the topic of the question (well, suddenly something comes to mind)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-09-07
@tsklab

Create a view with all databases like this:

CREATE VIEW [dbo].[View_All]
AS
SELECT 'Videoteq' AS BaseName, ID, LastName FROM Videoteq.dbo.Person
UNION
SELECT 'Phondex', ID, LastName FROM Phondex.dbo.Performer
GO

And make a request for it.
SELECT LastName
FROM View_All
WHERE (BaseName = 'Phondex') AND (ID = 512)

function is needed
So make a function with a request from the view.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question