SQL - 在Store过程中使用参数调用函数

时间:2015-07-30 14:26:35

标签: sql function select parameters

我有这样的情况:

SELECT CONVERT(VARCHAR(40), CONVERT(BIGINT, C1.cod_cta)), 
       C1.cod_cta, 
       C1.descripcio 
FROM   table1 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData' 
WHERE  C1.something = 1 
UNION ALL 
SELECT CONVERT(VARCHAR(40), CONVERT(BIGINT, C1.cod_cta)), 
       C1.cod_cta, 
       C1.descripcio 
FROM   table3 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData' 
WHERE  C1.something = 1 
       AND NOT EXISTS (SELECT 1 
                       FROM   table1 C3 
                       WHERE  C3.cod_cta = C1.cod_cta) 
UNION ALL 
SELECT CONVERT(VARCHAR(40), CONVERT(BIGINT, C1.cod_cta)), 
       C1.cod_cta, 
       C1.descripcio 
FROM   table3 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData' 
WHERE  C1.something = 1 
UNION ALL 
SELECT C1.importantfield COLLATE latin1_general_bin, 
       C1.importantfield COLLATE latin1_general_bin, 
       C1.desc_cuenta COLLATE latin1_general_bin 
FROM   table4 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData'  

我需要修改最后选择:

SELECT C1.importantfield COLLATE latin1_general_bin, 
       C1.importantfield COLLATE latin1_general_bin, 
       C1.desc_cuenta COLLATE latin1_general_bin 
FROM   table4 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData' 

The Field" ImportantField"可能有也可能没有面具。所以,我已经做了一个功能,如果它使用掩码,删除掩码。我的问题是如何在select中调用该函数。该功能需要一些参数才能工作。这些参数是来自另一个表的数据。我在另一个表中注册了" ImportantField"使用面具或不使用面具。所以,在SQL语言中,我需要这样的东西,但是工作;)

SELECT C1.importantfield COLLATE latin1_general_bin, 
       dbo.Removemask(C1.importantfield, (SELECT parameter1 
                                          FROM   table5), (SELECT parameter2 
                                                           FROM   table5), (SELECT parameter3 
                                                                            FROM table5)) COLLATE latin1_general_bin,
       C1.desc_cuenta COLLATE latin1_general_bin 
FROM   table4 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData' 

我从中获取参数的表只在行上。

任何想法我怎么能这样做?

提前致谢!!!

PS:我在SQL中真的很棒,请你好。

1 个答案:

答案 0 :(得分:1)

由于table5只包含一条记录,因此您可以将其加入查询而不会影响结果。这样可以轻松访问参数值 见下面的例子:

SELECT importantfieldOriginal = C1.importantfield COLLATE latin1_general_bin, 
       importantfieldWithOutMask = dbo.Removemask(C1.importantfield, table5.parameter1 , table5.parameter2, table5.parameter2)  COLLATE latin1_general_bin, 
       C1.desc_cuenta COLLATE latin1_general_bin 
FROM   table4 C1 
       JOIN (SELECT TOP 1 * 
             FROM   table2 
             ORDER  BY [date] DESC) C2 
         ON C2.somefield = 'SomeData' 
INNER JOIN table5 ON 1 = 1 

“1 = 1”伪造了table4和table5之间的关系,并且由于table5只包含一条记录,它就像是1:1的关系。 :)