sql 存储过程问题

[复制链接]
查看11 | 回复5 | 2011-3-28 07:00:59 | 显示全部楼层 |阅读模式
create proc se1
@id int,
@age int
as
begin
declare @sql int
set @sql=' select top 1 id from teacher'
print @sql
end
我想取到select top 1 id from teacher'返回值的id 但是总说varchar转化int失败


回复

使用道具 举报

千问 | 2011-3-28 07:00:59 | 显示全部楼层
create proc se1@id int output,--输出参数要加关键字output@age intasbegin set @id=(select top 1 id from teacher)end取值,要声明个变量:declare @Myidexec se1 @Myid output,30 --查询下变量的值:select @Myid 如果你想加上age变量的比较代码如下:create proc se1@id int output,--输出参数要加关键字output@age intasbegindelare @sql varChar(1000)set @sql=
回复

使用道具 举报

千问 | 2011-3-28 07:00:59 | 显示全部楼层
SQL Server通过 SP_EXECUTESQL的第2个参数来定义有哪些参数输出的加OUTPUT后面的参数为执行的时候,具体参数的数据。输出的加OUTPUT1> BEGIN2> DECLARE @Sql NVARCHAR(200);3> DECLARE @name VARCHAR(10);4> SET
回复

使用道具 举报

千问 | 2011-3-28 07:00:59 | 显示全部楼层
改成declare @sql nvarchar(200) set @sql=' select top 1 id from teacher'Exec sp_executesql@sql
回复

使用道具 举报

千问 | 2011-3-28 07:00:59 | 显示全部楼层
set @sql=' select top 1 id from teacher'去掉引号 set @sql=select top 1 id from teacher
回复

使用道具 举报

千问 | 2011-3-28 07:00:59 | 显示全部楼层
Exec(@sql)应该会返回一个记录集, 试看
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行