I want my code in a T-SQL function to dynamically reference tables based on database names passed in via a parameter. For example, If i pass in 'MASTER' the function will look at the master database for the table, if i pass in 'PUBS' it will look in the PUBS database for the table to select from etc.
code snippet attached. In code snippet, the "TWO" is a hard coded reference to a database. i want to be able to specify the database dynamically based on a parameter.
The accepted answer must be able to select from a database dynamically AND return value from the results set via the function
DECLARE @FiscalDate DATETIME
SELECT @FiscalDate =
DATEDIFF (DD, TWO.DBO.SY40101.FSTFSCDY,
CAST(TWO.DBO.SY40101.Year1 AS VARCHAR) + '/1/1'
WHERE TWO.DBO.SY40101.YEAR1 IN (
WHERE @DOCDATE BETWEEN TWO.DBO.SY40101.FSTFSCDY AND TWO.DBO.SY40101.LSTFSCDY