我收到以下错误:
将数据类型varchar转换为bigint时出错。
执行以下查询时:
declare @MaxSizeMB BIGINT
set @MaxSizeMB = 50
DECLARE @Query varchar(8000)=''
set @Query = 'UPDATE SPMC
SET SPMC.HasBlockedSize = 1, SPMC.HasIssues = 1
FROM PWI
INNER JOIN SI ON SI.Id = PWI.SourceItemId
INNER JOIN SPMC ON SPMC.SourceItemId = SI.Id
AND SI.ItemSize > ' + CAST(@MaxSizeMB AS bigint) + ' * 1024 * 1024 '
print (@Query)
答案 0 :(得分:1)
您不计算查询中的@MaxSizeMB x 1024 x 1024
,只是将结果连接到您的查询,如下所示:
declare @MaxSizeMB BIGINT
set @MaxSizeMB = 50*1024*1024
DECLARE @Query varchar(8000)=''
set @Query=
'Update SPMC
Set SPMC.HasBlockedSize = 1, SPMC.HasIssues = 1
FROM PWI
INNER JOIN SI on SI.Id = PWI.SourceItemId
INNER JOIN SPMC on SPMC.SourceItemId = SI.Id
AND SI.ItemSize > '+ cast(@MaxSizeMB as varchar(max))
print (@Query)
您也可以在不使用动态SQL的情况下获得相同的结果。如下:
Update SPMC
Set SPMC.HasBlockedSize = 1, SPMC.HasIssues = 1
FROM PWI
INNER JOIN SI on SI.Id = PWI.SourceItemId
INNER JOIN SPMC on SPMC.SourceItemId = SI.Id
AND SI.ItemSize > (@MaxSizeMB *1024*1024)
答案 1 :(得分:0)
由于MaxSizeMB
已经BIGINT
将其转换为BIGINT
无效,您应该将计算出的值转换为varchar(max)
。
declare @MaxSizeMB BIGINT
set @MaxSizeMB = 50
DECLARE @Query varchar(8000)=''
set @Query=
'Update SPMC
Set SPMC.HasBlockedSize = 1, SPMC.HasIssues = 1
FROM PWI
INNER JOIN SI on SI.Id = PWI.SourceItemId
INNER JOIN SPMC on SPMC.SourceItemId = SI.Id
AND SI.ItemSize > '+ cast(@MaxSizeMB * 1024 * 1024 as varchar(max))
print (@Query)
答案 2 :(得分:0)
试试这个:
declare @MaxSizeMB BIGINT
set @MaxSizeMB = 50
DECLARE @Query varchar(8000)=''
set @Query=
'Update SPMC
Set SPMC.HasBlockedSize = 1, SPMC.HasIssues = 1
FROM PWI
INNER JOIN SI on SI.Id = PWI.SourceItemId
INNER JOIN SPMC on SPMC.SourceItemId = SI.Id
AND SI.ItemSize > '+ cast(cast(@MaxSizeMB as bigint) as varchar(max)) +' * 1024 * 1024 '
print (@Query)
答案 3 :(得分:0)
如果要生成动态查询,请将@MaxSizeMB
强制转换为VARCHAR:
declare @MaxSizeMB BIGINT
set @MaxSizeMB = 50
DECLARE @Query VARCHAR(8000)=''
set @Query=
'Update SPMC
Set SPMC.HasBlockedSize = 1, SPMC.HasIssues = 1
FROM PWI
INNER JOIN SI on SI.Id = PWI.SourceItemId
INNER JOIN SPMC on SPMC.SourceItemId = SI.Id
AND SI.ItemSize > '+ cast(@MaxSizeMB as varchar) +' * 1024 * 1024 '
print (@Query)