创建需要几个参数的表值函数

时间:2019-05-15 12:12:51

标签: tsql

我的T-SQL代码需要一些帮助。背景我有一个表,其中BY YEAR数据按列输出。因此,单行将具有CN [Year 0] ........ CN [Year 10],Premiums [Year 0] ... [Year 10]等。我正在尝试编写一个函数它使用一个整数参数,然后仅返回指定年份列的表,例如CN,Premium等。最终,我希望在某些时候连接这些表。我具有手动运行代码的基本知识,但不确定如何自动化我的函数,以便它可以返回表。

目前,我可以在其中运行代码 - - - 从这里 - - - 到这里 手动但不自动执行代码,以便它可以返回表

CREATE FUNCTION [xxxxxxx_Price_Schema].get_MultiYear(@year varchar(1))
RETURNS TABLE
AS
RETURN
------from here 
declare @year varchar(1);
declare @yearNo varchar(1);
declare @customerNo varchar(25);
declare @CN varchar(3);
declare @SQLString NVARCHAR(max);
declare @SQLResult nvarchar(max);

set @year = 1
set @yearNo = cast(@year as varchar(1))
set @customerNo = QUOTENAME(cast('CustomerNumber[Year '+ @yearNo + ']' as varchar(25)))
set @CN = concat('CN',@yearNo)

set @SQLString = 
            '
            select distinct
            [CN[Year '+ @yearNo +']]] AS CN
            ,[Premium[Year '+ @yearNo + ']]] AS Premium
            ,[Age[Year '+ @yearNo + ']]] AS Age
            ,[Sex[Year '+ @yearNo + ']]] AS Sex
            FROM [Test].[Test202002].[PHL_MultiYear_Test_Output] 
              WHERE ' + @customerNo + ' = '''+ @CN + ''' 
             '

EXECUTE sp_executesql @SQLString
----too here

RETURN @SQLString

END
GO

enter image description here

如果我的输入参数为0,则期望结果 enter image description here

1 个答案:

答案 0 :(得分:3)

如果没有Dynamic SQL,将无法获得动态列名。但是,如果您不希望标准化结果,请考虑取消数据

可以很容易地将其合并到表值函数中。

示例dbFiddle

Declare @Year varchar(1) = '0'

Select SelYear = @Year
      ,CN      = max(case when Item like 'CN%'      then Value end)
      ,Premium = max(case when Item like 'Premium%' then Value end)
 From  (
         Select *
          From  (Select * 
                       ,RN=Row_NUmber() over (Order by (Select null)) 
                 From  YourTable 
                 Where 1=1 -->>> Put your actual WHERE here <<<--
                ) src
          Unpivot ( Value for Item in ([CN [Year 0]]],[CN [Year 1]]],[Premium [Year 0]]],[Premium [Year 1]]] )) unp  -->>> Add 2 through 5 <<<--
          Where Item like '%'+@Year+']'
       ) A
 Group By RN

返回

SelYear CN  Premium
0       0   10
0       0   20
0       0   30
0       0   40

已更新为奇数列名称dbFiddle

编辑#2-变量数据类型

Select SelYear = @Year
      ,CN      = max(case when Item like 'CN%'      then Value end)
      ,Premium = max(case when Item like 'Premium%' then Value end)
 From  (
     Select *
      From  (Select * 
                   ,RN=Row_NUmber() over (Order by (Select null)) 
         From  YourTable 
         Where 1=1 -->>> Put your actual WHERE here <<<--
        ) src
      Cross Apply (  values ('CN [Year 0]'     ,convert(varchar(50),[CN [Year 0]]]) )
                           ,('CN [Year 1]'     ,convert(varchar(50),[CN [Year 1]]]) )
                           ,('Premium [Year 0]',convert(varchar(50),[Premium [Year 0]]]) )
                           ,('Premium [Year 1]',convert(varchar(50),[Premium [Year 0]]]) )
                  )B(Item,Value)
      Where Item like '%'+@Year+']'
       ) A
 Group By RN