这段简单的动态创建的查询代码有什么问题??

dxhh99 2005-04-12 11:35:06
declare @f1 decimal(6,2)
select @f1=合计费用 from 一月份 where 电话号码=5951355
select @f1
成功运行


declare @f1 decimal(6,2)
declare @s varchar(100)
set @s='select @f1=合计费用 from 一月份 where 电话号码=5951355'
exec(@s)
select @f1
运行是提示变量f1没有定义。

...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Well 2005-04-14
  • 打赏
  • 举报
回复

declare @s varchar(100)
Set @s=N'declare @f1 decimal(6,2)'
Set @s=@s+N'select @f1=合计费用 from 一月份 where 电话号码=5951355'
Set @s=@s+N'select @f1'
exec(@s)
mouseanAnya 2005-04-12
  • 打赏
  • 举报
回复
啊,已经有回复了,当我没来过
talantlee 2005-04-12
  • 打赏
  • 举报
回复

declare @s varchar(100)
set @s=N' declare @f1 decimal(6,2)
select @f1=合计费用 from 一月份 where 电话号码=5951355'
print @f1'
exec(@s)

mouseanAnya 2005-04-12
  • 打赏
  • 举报
回复
declare @s varchar(100)
set @s='declare @f1 decimal(6,2);select @f1=合计费用 from 一月份 where 电话号码=5951355;select @f1'
exec(@s)
paoluo 2005-04-12
  • 打赏
  • 举报
回复
你可以这样。

第二段代码改为


declare @s varchar(100)
Set @s=N'declare @f1 decimal(6,2)'
Set @s=@s+N'select @f1=合计费用 from 一月份 where 电话号码=5951355'
Set @s=@s+N'select @f1'
exec(@s)
paoluo 2005-04-12
  • 打赏
  • 举报
回复
因为你的declare @f1 decimal(6,2),在@s这段语句之外,所以会提示变量未定义。

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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