0

我正在创建一个使用函数 (usr.udf_overpunch) 将特殊字符转换为数值的查询。如何将 col4 的结果设置为用于函数中输入数据的参数 @incomming?

declare @incomming nvarchar(15)
drop table #tempgb
SELECT *
into #tempgb
  FROM [AdhocTeamC].[dbo].[R0530135.RPT.SPON_INVOICE]
where substring(col1, 1,5) = 'TPAMT'

select col1,
substring(col1, 1,5) as col2,
substring(col1, 38,5) as col3,
@incomming = substring(col1, 78,14) as col4,
[usr].[Udf_OverPunch] (@incomming) as Overpunch
from #tempgb
4

1 回答 1

0

您不能将 assignment ( @incomming = ...) 与常规SELECT. 看起来你正试图让自己免于拥有substring(col1, 78, 14)两次;如果是这种情况,试试这个:

;WITH CTE AS (
    SELECT
        col1,
        SUBSTRING(col1, 1, 5) AS COL2,
        SUBSTRING(col1, 38, 5) AS COL3,
        SUBSTRING(col1, 78, 14) AS COL4
    FROM
        #tempgb
)
SELECT
    *,
    usr.Udf_OverPunch(col4) AS Overpunch
FROM
    CTE;

如果您不在乎是否有substring(col1, 78, 14)两次,请直接发送并为自己保存 CTE:

SELECT
    col1,
    SUBSTRING(col1, 1, 5) AS COL2,
    SUBSTRING(col1, 38, 5) AS COL3,
    SUBSTRING(col1, 78, 14) AS COL4,
    usr.Udf_OverPunch(SUBSTRING(col1, 78, 14)) AS Overpunch
FROM
    #tempgb
于 2013-09-19T18:55:30.087 回答