0

我有一个表,其中有 40 多列都存储在 varchar 中。某些列也包含数字或日期的数据。

我有一个用户定义的函数,它将列名和数据类型作为参数,并尝试验证列中的数据。

如果验证失败,它应该返回一条消息说“列名不是数字或缺失”。但是它返回“值不是数字或缺失”。

函数代码为:

create function [dbo].[fn_Polaris_Validate_Staging_Data](@ColumnName varchar(max), @TargetValidation varchar(max))
returns varchar(max)
AS
Begin
Declare @Remark varchar(max);
    if(@TargetValidation = 'Standard' and @ColumnName is null)
    BEGIN
        select @Remark = @ColumnName + ' is not provided.'
        return @Remark
    END
    if(@TargetValidation = 'NumericType' and ISNUMERIC(@ColumnName) = 0)
    BEGIN
        select @Remark = @ColumnName + ' is not numeric or not provided.'
        return @Remark
    END
    if(@TargetValidation = 'DateType' and ISDATE(@ColumnName) = 0)
    BEGIN
        select @Remark = @ColumnName + ' is not proper date or not provided.'
        return @Remark
    END
    return isnull(@Remark, '')
END

我的表中有一个名为“备注”的列,用于保存此备注。这是我在更新查询中使用此函数的方式:

UPDATE TABLE TABLE1
SET remarks = isnull(remarks,'') +
              function(Interest, 'NumericType') +
              function(Asset, 'Standard') +
              function(Date, 'DateType')

任何帮助将不胜感激。谢谢你。

4

1 回答 1

0

usingISNUMERIC(@variable)仅适用于该@variable 的字符串内容,它不会引用字符串中的任何列名。

declare @col as varchar(max)
set @col = '1'
select ISNUMERIC(@col)
= 1

declare @col as varchar(max)
set @col = 'table.id'
select ISNUMERIC(@col)
= 0 - regardless of the type of table.id

也许这会有用?

SELECT DATA_TYPE FROM information_schema.columns 
WHERE Table_Name='mytable' and Column_Name='mycolname'
于 2013-02-28T12:54:20.463 回答