我刚学SQL,有一个问题请教各位。
基本信息表中“内容”列为<Long Text>,我想把“内容”内“销售价五”中的值改为商品信息表中的“进货价”列中的值。。我写了下面语名运行不过去。请各位指导。。
UPDATE 基本信息
SET 内容 = REPLACE(cast(内容 AS varchar(8000)), SUBSTRING(内容,
CHARINDEX('销售价五="', 内容, 0) + 5, CHARINDEX('"', 内容, CHARINDEX('销售价五="',
内容, 0) + 5) - (CHARINDEX('销售价五="', 内容, 0) + 5)), a.进货价)
FROM 基本信息 INNER JOIN 商品信息 a ON a.ID = 基本信息.ID
基本信息表中“内容”列为<Long Text>,我想把“内容”内“销售价五”中的值改为商品信息表中的“进货价”列中的值。。我写了下面语名运行不过去。请各位指导。。
UPDATE 基本信息
SET 内容 = REPLACE(cast(内容 AS varchar(8000)), SUBSTRING(内容,
CHARINDEX('销售价五="', 内容, 0) + 5, CHARINDEX('"', 内容, CHARINDEX('销售价五="',
内容, 0) + 5) - (CHARINDEX('销售价五="', 内容, 0) + 5)), a.进货价)
FROM 基本信息 INNER JOIN 商品信息 a ON a.ID = 基本信息.ID
不允许从数据类型 money 到 nvarchar 的隐性转换。请使用 CONVERT 函数来运行此查询。
as
begin
Declare @Sql nvarchar(4000)
declare @p_s varchar(800)
Declare @p binary(16),@postion int,@rplen int
set @Sql=N'Select @p=textptr('+@vField+'),@rplen=len('''+@vBefore+'''),@postion=patindex(''%'+@vBefore+'%'','+@vField+')-1 from '+@vTable
exec sp_executesql @Sql,N'@p binary(16) output,@rplen int output,@postion int output ',@p output,@rplen output,@postion output
while @postion >= 0
begin
exec master.dbo.xp_varbintohexstr @p, @p_s out
set @Sql=N'updatetext '+@vTable+'.'+@vField+' '+@p_s+' '+cast(@postion as varchar(8))+' '+cast(@rplen as varchar(4))+' '''+@vAfter+''''
exec(@Sql)
set @Sql=N'select @postion=patindex(''%'+@vBefore+'%'','+@vField+')-1 from '+@vTable
exec sp_executesql @Sql,N'@postion int output ',@postion output
end
end
INSERT t SELECT '', 'aa北京中华人民共和国北京天安门北京asdsdf'+REPLICATE('a', 8000)
select * from t
exec p_replacentext 't','col2','aa','首都'
select * from t
drop table t