我有一个存储过程,里面要执行两条sql语句,其中第二条的where条件是根据第一个sql语句的结果得到的。应该怎么办呢?假设如下:语句一: select a from table1 where ...
变量 a = 语句一的执行结果(这里假设返回值结果唯一)
语句二:selece * from table2 where field1 = a
变量 a = 语句一的执行结果(这里假设返回值结果唯一)
语句二:selece * from table2 where field1 = a
selece * from table2 where field1 =(select a from table1 where ...)selece * from table2 where field1 in(select a from table1 where ...)
as
selece * from table2 where field1 =(select a from table1 where ...) --a 是唯一值。--two
create proc a
as
selece * from table2 where field1 in(select a from table1 where ...) --a 不是唯一值。
select a from table1 where
取到的值放到一个变量里好做判断等用途呢?
可不可以把
select a from table1 where
取到的值放到一个变量里好做判断等用途呢?declare @v varchar(100)
select @V=a from table1 where--这个要唯一
变量 a = 语句一的执行结果(这里假设返回值结果唯一)
语句二:selece * from table2 where field1 = a
===================================================
declare @a varchar(50)--保存语句1执行结果
select @a = a from table1 where ...--执行第一条语句,并把字段A的值给变量@a
selece * from table2 where field1 = @a
@UserId varchar(50)
as
Declare @sqlText Varchar(1000)
Declare @RuleIds varchar(500)
set @sqlText = N'SELECT @RuleIds = 角色编号 FROM 用户信息表 where 用户编号 ='+'''' +@UserId+''''
EXECUTE(@sqlText) print @RuleIds-------------------------结果报错--------------------------
服务器: 消息 137,级别 15,状态 1,行 1
必须声明变量 '@RuleIds'。