sq_executesql语法错误

jiaai 2010-03-22 09:30:47
exec sp_executesql N'update l_goodsbom set goodsposition=@P1,substitute=3268,ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1) where goodsid=3934 and @P2
', N'@P1 varchar(64),@P2 varchar(64)', 'FOR C3 BTM', 'charindex(''FOR C3 BTM替代料'',rtrim(ltrim(memo)))<>0'
在sql查询分析器中报@P2附近有语法错误,请各位帮忙看看!
...全文
136 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaai 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 roy_88 的回复:]
SQL code
declare @P2 nvarchar(1000)

set @P2='charindex(''FOR C3 BTM替代料'',rtrim(ltrim(memo)))<>0'

declare @s nvarchar(4000)
set @s=N'update l_goodsbom set goodsposition=@P1,substitute=3268,ecn=……
[/Quote]

应改成如下所示
declare @P2 nvarchar(1000)

set @P2='charindex(''FOR C3 BTM替代料'',rtrim(ltrim(memo)))<>0'

declare @s nvarchar(4000)
set @s=N'update l_goodsbom set goodsposition=@P1,substitute=3268,ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1) where goodsid=3934 and '+@P2
exec sp_executesql @s, N'@P1 varchar(64)', 'FOR C3 BTM'
ChinaJiaBing 2010-03-22
  • 打赏
  • 举报
回复

---try


exec sp_executesql N'update l_goodsbom set goodsposition=@P1,substitute=3268,
ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1) where goodsid=3934 and charindex(@p1,rtrim(ltrim(memo)))>0',
N'@P1 varchar(64)',
@P1='FOR C3 BTM'

中国风 2010-03-22
  • 打赏
  • 举报
回复
分兩部份,把條件部分字符串先拼上
中国风 2010-03-22
  • 打赏
  • 举报
回复
declare @P2 nvarchar(1000)

set @P2='charindex(''FOR C3 BTM替代料'',rtrim(ltrim(memo)))<>0'

declare @s nvarchar(4000)
set @s=N'update l_goodsbom set goodsposition=@P1,substitute=3268,ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1) where goodsid=3934 and '+@P2
exec sp_executesql @s, N'@P1 varchar(64),@P2 varchar(64)', 'FOR C3 BTM'
htl258_Tony 2010-03-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jiaai 的回复:]
还是报第 4 行: '@P2' 附近有语法错误。
[/Quote]

exec sp_executesql N'
update l_goodsbom set
goodsposition=@P1,substitute=3268,ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1)
where goodsid=3934 and charindex(@P2,rtrim(ltrim(memo)))<>0
',
N'@P1 nvarchar(64),@P2 nvarchar(64)', '''FOR C3 BTM''', '''FOR C3 BTM替代料'''
honkerhero 2010-03-22
  • 打赏
  • 举报
回复
and @P2

不能这样写吧?


既然知道变量的值,拼SQL字符串,最后执行不带参的吧,AND @P2本身就不合SQL语法,就像select @C from talbe, @C是一个列名一样
jiaai 2010-03-22
  • 打赏
  • 举报
回复
还是报第 4 行: '@P2' 附近有语法错误。
htl258_Tony 2010-03-22
  • 打赏
  • 举报
回复
重点是:'''FOR C3 BTM'''
htl258_Tony 2010-03-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 jiaai 的回复:]
exec sp_executesql N'update l_goodsbom set goodsposition=@P1,substitute=3268,ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1) where goodsid=3934 and @P2
', N'@P1 varchar(64),@P2 varchar(64)', 'FOR……
[/Quote]

exec sp_executesql N'
update l_goodsbom set
goodsposition=@P1,substitute=3268,ecn=right(memo,len(memo)-charindex(''Ecn'',memo)+1)
where goodsid=3934 and @P2
',
N'@P1 nvarchar(64),@P2 nvarchar(64)', '''FOR C3 BTM''', 'charindex(''FOR C3 BTM替代料'',rtrim(ltrim(memo)))<>0'
TRY
zpzpyesterday 2010-03-22
  • 打赏
  • 举报
回复

and @P2


这个后面没有写完吧?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧