关于sql 赋值的问题

fstao 2025-01-11 22:49:53

数据库是sqlserver 2008,代码如下:

declare @sql varchar(3000),@code varchar(200),@strcode varchar(200)

delete from tmp_table
insert into tmp_table(code)
select '98001'
union all 
select '98002'
union all
select '98003'

set @code='98003'

set @sql='select code from tmp_table where code='+@code

exec(@sql)

这样会显示:

code
98003

我在上面定义了“declare @strcode varchar(200) ”,我想把“exec(@sql)”的数据赋值到@strcode,比如“exec(@sql)”,值是‘98003’,把‘98003’赋值给@strcode,如何来写代码?

...全文
177 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
雨夹雪 01-13
  • 打赏
  • 举报
回复

declare @code varchar(200),@strcode varchar(200)
 
delete from tmp_table
insert into tmp_table(code)
select '98001'
union all 
select '98002'
union all
select '98003'
 
set @code='98003'
 
EXEC sp_executesql N'select @strcode=code from tmp_table where code=@code',N'@code varchar(200),@strcode  varchar(200) output',@code,@strcode OUTPUT
SELECT @strcode

34,837

社区成员

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

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