Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks for the info, Mladen, that tip is good to know :)</p> <p>But while that helps me call a function residing in master from in the current database context "ContextCurrent", what I really want is to be able to call the scalar-valued function from the context of the source database "ContextSource".</p> <p>While in general, I have my reservations against dynamic sql, I ended up using it here as follows.</p> <p><code> <br/>DECLARE @sourceDbName nvarchar(128) <br/>SET &nbsp; &nbsp; @sourceDbName = N'sbaportia1' <br/> <br/>DECLARE @strQuery nvarchar(max) <br/>DECLARE @parmDefn nvarchar(max) <br/>DECLARE @DbtsCapture varbinary(8) <br/>SET &nbsp; &nbsp;@strQuery = <br/> &nbsp; &nbsp;' <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; N' ' + N'USE' + N' ' + @sourceDbName + N' ' <br/> &nbsp; &nbsp; &nbsp; &nbsp; + N' ' + N'SELECT @dbtsCapture = min_active_rowversion()' <br/> &nbsp; &nbsp;' <br/> <br/>SET @parmDefn = <br/> &nbsp; &nbsp;N' <br/> &nbsp; &nbsp; &nbsp; &nbsp; @dbName varchar(128), <br/> &nbsp; &nbsp; &nbsp; &nbsp; @dbtsCapture varbinary(8) OUTPUT <br/> &nbsp; &nbsp;' <br/> <br/>EXEC sp_executesql &nbsp;@strQuery <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;,@parmDefn <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;,@dbName = 'autobahn' <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;,@dbtsCapture = @dbtsCapture OUTPUT <br/> <br/>SELECT @dbtsCapture </code></p> <p>Furthermore, since sp_executesql runs in a separate thread, the database context within the script below will be automatically be the same upon the exit of sp_executesql as upon the entry of sp_executesql. (I learned waaayy too much about sp_executesql in the early 2000's.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload