0

我正在尝试创建一个视图,其中列连接以生成导入行。

这是我的声明:

SET NOCOUNT ON;
declare @SQL as varchar(4000)
        --@CD as date

--set @CD = convert(varchar(30), cast(getdate() as date), 110)


    -- Insert statements for procedure here
set @SQL = 'CREATE VIEW vw_GCS_Export
    As
    select division, [primary image id],[Item Number], [Brand Name],[Marketing Description],
    [Colours]as Colors,[Live Date],[Sample Type],substring([FileName],charindex('+ CHAR(39) + '_' + CHAR(39) +',[FileName],1)+1,CHARINDEX('+ CHAR(39) + '.' + CHAR(39) +',[FileName],1)-5) as BatchID,
    '+ CHAR(39) + '670' + CHAR(39) +' as Status, [Primary image ID] + '+ CHAR(39) + '_P' + CHAR(39) +' as [Shot Name],
    Cast(Null as varchar(50))as [Swatch/Variation],Cast(Null as varchar(50))as [Alternate Shot],[UserName],''' + CAST(convert(date,Sysdatetime(),110)as varchar(10)) + ''' as [Import Date],
    Cast(Null as varchar(50))as [Return Instructions],Cast(Null as varchar(50))as [Rush Request], DATEADD(WEEKDAY,-5,[live date]) as [ImageDeliveryDate],
    '+ CHAR(39) + 'Division '+ CHAR(39) + '+ [division] +'+ CHAR(39) + ' * ' + CHAR(39) +' + [UserName] as JobName
    from Sheet1$'


EXEC (@SQL)
select * from vw_GCS_Export

END

当我执行存储过程时出现错误:

将数据类型 varchar 转换为浮点数时出错。

我是否需要nvarchar(50)使用 cast 将列定义为或类似的东西?

4

1 回答 1

0

n本题缺少很多信息,比如[Sheet1$]中各列的数据类型。不过,我可以给你一个一般性的解释。每当表达式尝试将浮点值与字符串值连接时,您都会收到此错误。您可能期望自动转换是将浮点数转换为字符串然后连接,但实际上发生的情况是 MSSQL 尝试将字符串转换为数值,然后将其添加到浮点数中。正如 Love2Learn 所说,您需要将数值显式转换/转换为字符串,例如

cast([Primary image ID] as varchar(max)) + '_P' as [Shot Name]
'Division '+ cast([division] as varchar(max)) +' * ' + [UserName] as JobName
于 2013-04-01T20:46:14.910 回答