当前位置: 代码迷 >> Sql Server >> 一个简单的有关问题(,解决马上给分)
  详细解决方案

一个简单的有关问题(,解决马上给分)

热度:175   发布时间:2016-04-27 19:41:31.0
一个简单的问题(在线等,解决马上给分)
declare   @s   varchar(8000)
declare   @e   varchar(8000)
declare   @i   int
Select   @e   =   ddbh   From   kjnd   ,   mzmxbjzjg   Where   kjnd.bh   =   mzmxbjzjg.kjndbh   and   kjnd.sybz   =   1
Set   @s   =   'Select   Count(*)     From   ddjgzl   Where   ddbh   in   ( '[email protected]+ ')   AND   ddbh   =   103 '

Return   exec(@S)

以上语句   我怎么取到   EXEC(@S)   的值  


------解决方案--------------------
declare @s varchar(8000)
declare @e varchar(8000)
declare @i int
Select @e = ddbh From kjnd , mzmxbjzjg Where kjnd.bh = mzmxbjzjg.kjndbh and kjnd.sybz = 1
Select @i =Count(*) From ddjgzl Where ddbh in (
Select ddbh From kjnd , mzmxbjzjg Where kjnd.bh = mzmxbjzjg.kjndbh and kjnd.sybz = 1
) AND ddbh = 103

Return @i
------解决方案--------------------
如果是在函数中
declare @s varchar(8000)
declare @e varchar(8000)
declare @i int
Select @i =Count(*) From ddjgzl Where ddbh in (
Select ddbh From kjnd , mzmxbjzjg Where kjnd.bh = mzmxbjzjg.kjndbh and kjnd.sybz = 1
) AND ddbh = 103

Return @i

如果是在存储过程中

declare @s nvarchar(8000)
declare @e nvarchar(8000)
declare @i int
set @e= 'Select @i =Count(*) From ddjgzl Where ddbh in (
Select ddbh From kjnd , mzmxbjzjg Where kjnd.bh = mzmxbjzjg.kjndbh and kjnd.sybz = 1
) AND ddbh = 103 '

exec sp_execute @e,N '@i int output ',@i output
select @i

------解决方案--------------------
--不好意思,没有改数据类型,你现在看一下
declare @s nvarchar(8000)
declare @e nvarchar(8000),@n int
declare @i int
Select @e = ddbh From kjnd , mzmxbjzjg Where kjnd.bh = mzmxbjzjg.kjndbh and kjnd.sybz = 1
Set @s = 'Select @n = Count(*) From ddjgzl Where ddbh in ( '[email protected]+ ') AND ddbh = 103 '

exec sp_executesql @s,N '@n int out ',@n out

select @n
--Return exec(@S)
  相关解决方案