将数据库名称作为参数传递给内联函数

时间:2014-09-10 23:00:12

标签: sql sql-server-2012 inline

我正在尝试创建一个可以在几十个存储过程中使用的内联函数,而不是将代码放在每一个中。该函数可以由各种数据库调用,因此我需要能够将数据库名称作为参数传递。在这种情况下,参数是@dbname。这是我的代码:

CREATE FUNCTION [dbo].[fn_units]
     (
      @inReportControlId int,
      @dbname varchar(30)
     )

RETURNS table
AS

RETURN 

(      

WITH prep AS (
  SELECT *
  FROM
  (
  SELECT RTRIM(cp.Label) + 'ReportUnit' TYPE, RTRIM(cu.Label) value, rc.ReportControlId, curr.Symbol, curp.Prefix, curp.Factor
  FROM   @dbname.CorpConventionUnit AS ccu INNER JOIN
                         @dbname.CorpUnit AS cu ON ccu.NUnitId = cu.CorpUnitId INNER JOIN
                         @dbname.CorpProduct AS cp ON ccu.CorpProductId = cp.CorpProductId INNER JOIN
                         PhdReports.PhdRpt.ReportControl AS rc ON ccu.CorpConventionId = rc.ConventionId INNER JOIN
                         @dbname.CorpUnit AS cu2 ON cu2.CorpUnitId = cu.CorpBaseUnitId INNER JOIN
                         @dbname.CorpCurrency AS curr ON rc.CurrencyId = curr.CorpCurrencyId INNER JOIN
                         @dbname.CorpCurrencyPrefix AS curp ON rc.CurrencyPrefix = curp.Prefix  

  UNION ALL
  SELECT RTRIM(cp.Label) + 'BaseUnit' TYPE, RTRIM(cu2.Label) value, rc.ReportControlId, curr.Symbol, curp.Prefix, curp.Factor
  FROM   @dbname.CorpConventionUnit AS ccu INNER JOIN
                         @dbname.CorpUnit AS cu ON  ccu.NUnitId = cu.CorpUnitId INNER JOIN
                         @dbname.CorpProduct AS cp ON ccu.CorpProductId = cp.CorpProductId INNER JOIN
                         PhdReports.PhdRpt.ReportControl AS rc ON ccu.CorpConventionId = rc.ConventionId INNER JOIN
                        @dbname.CorpUnit AS cu2 ON cu2.CorpUnitId = cu.CorpBaseUnitId INNER JOIN
                         @dbname.CorpCurrency AS curr ON rc.CurrencyId = curr.CorpCurrencyId INNER JOIN
                         @dbname.CorpCurrencyPrefix AS curp ON rc.CurrencyPrefix = curp.Prefix

  UNION ALL 
  SELECT RTRIM(cp.Label) + 'ReportUnitDenom' TYPE, RTRIM(cu.Label) value, rc.ReportControlId, curr.Symbol, curp.Prefix, curp.Factor
  FROM   @dbname.CorpConventionUnit AS ccu INNER JOIN
                         @dbname.CorpUnit AS cu ON ccu.DUnitId = cu.CorpUnitId INNER JOIN
                         @dbname.CorpProduct AS cp ON ccu.CorpProductId = cp.CorpProductId INNER JOIN
                         PhdReports.PhdRpt.ReportControl AS rc ON ccu.CorpConventionId = rc.ConventionId INNER JOIN
                         @dbname.CorpUnit AS cu2 ON cu2.CorpUnitId = cu.CorpBaseUnitId INNER JOIN
                         @dbname.CorpCurrency AS curr ON rc.CurrencyId = curr.CorpCurrencyId INNER JOIN
                         @dbname.CorpCurrencyPrefix AS curp ON rc.CurrencyPrefix = curp.Prefix 
  )AS hi
  GROUP BY  hi.ReportControlId ,value, TYPE, hi.Symbol, hi.Prefix, hi.Factor

)



  Select ReportControlID,Symbol,Prefix, Factor,
     Min(Case TYPE When 'OilBaseUnit' Then value End) OilBaseUnit,
     Min(Case TYPE When 'NGLBaseUnit' Then value End) NGLBaseUnit,
     Min(Case TYPE When 'GasBaseUnit' Then value End) GasBaseUnit,
     Min(Case TYPE When 'OilReportUnit' Then value End) OilReportUnit,
     Min(Case TYPE When 'NGLReportUnit' Then value End) NGLReportUnit,
     Min(Case TYPE When 'GasReportUnit' Then value End) GasReportUnit,
     Min(Case TYPE When 'GORReportUnit' Then value End) GORReportUnit,
     Min(Case TYPE When 'GORReportUnitDenom' Then value End) GORReportUnitDenom,
     Min(Case TYPE When 'YieldReportUnit' Then value End) YieldReportUnit,
     Min(Case TYPE When 'YieldReportUnitDenom' Then value End) YieldReportUnitDenom,
     Min(Case TYPE When 'CondensateBaseUnit' Then value End) CondensateBaseUnit,
     Min(Case TYPE When 'CondensateReportUnit' Then value End) CondensateReportUnit
   From prep
   where ReportControlId = @inReportControlId
   Group By ReportControlId, Symbol,Prefix, Factor

通常,如果这只是在存储过程中,我会将查询存储在varchar中,并使用replace命令将@dbname替换为当时使用的数据库。这似乎不适用于内联函数。所以我想我可以将dbname作为参数传递给函数,但是它不会让我保存函数,因为它不能识别" @dbname"作为有效的数据库。

如何用传入的参数替换变量?

0 个答案:

没有答案