存储过程里SQL语句执行的问题

ldwlqy 2003-02-28 09:01:18
DECLARE @SQLCond varchar(500)

Set @SQLCond='Select UnitID From tblUnits Where ParentID=1234'

If EXISTS (Select UnitID From tblUnits Where ParentID In (@SQLCond) And UnitID<>ParentID)
print 'OK'
else
print 'No'


存储过程里Select语句能不能这样写?变量@SQLCond加进去整个语句执行会有什么问题吗?
...全文
31 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaobochina 2003-02-28
  • 打赏
  • 举报
回复
create table #tblUnits
(
unitid varchar(10),
parentID varchar(10)

)

insert #tblunits
values(1,'1234')

DECLARE @SQLCond varchar(500)

Set @SQLCond='Select UnitID From #tblUnits Where ParentID=1234'

--select @SQLCond=UnitID From #tblUnits WHere ParentID='1234'
select @SQLCond
If EXISTS (Select UnitID From #tblUnits Where ParentID In (@SQLCond) And UnitID<>ParentID)
print 'OK'
else
print 'No'

drop table #tblUnits
gaobochina 2003-02-28
  • 打赏
  • 举报
回复

insert #tblunits
values(1,'1234')

DECLARE @SQLCond varchar(500)

Set @SQLCond='Select UnitID From #tblUnits Where ParentID=1234'
--此举应为 select @SQLCond=UnitID From #tblUnits Where ParentID=1234'
select @SQLCond
If EXISTS (Select UnitID From #tblUnits Where ParentID In (@SQLCond) And UnitID<>ParentID)
print 'OK'
else
print 'No'
drop table #tblUnits



ldwlqy 2003-02-28
  • 打赏
  • 举报
回复
CrazyFor(蚂蚁)

Set @SQLCond='Select UnitID From tblUnits Where ParentID=1234'

exec('If EXISTS (Select UnitID From tblUnits Where ParentID In ('+@SQLCond+') And UnitID<>ParentID)
print 'OK'
else
print 'No'

这样可以
但是我判断后还有从新给变量 @SQLCond 赋值
set @SQLCond=......
语句该怎样写呢?
ldwlqy 2003-02-28
  • 打赏
  • 举报
回复
改为
EXECUTE sp_executesql @SQLCond

报错
必须声明变量 '@IDCount'
CrazyFor 2003-02-28
  • 打赏
  • 举报
回复
TRY:
Set @SQLCond='Select UnitID From tblUnits Where ParentID=1234'

exec('If EXISTS (Select UnitID From tblUnits Where ParentID In ('+@SQLCond+') And UnitID<>ParentID)
print 'OK'
else
print 'No'
')

chpeng 2003-02-28
  • 打赏
  • 举报
回复
先将exec(@SQLCond)插入临时表,
再执行下面语句
ldwlqy 2003-02-28
  • 打赏
  • 举报
回复
glboy(星毅)

@str_sqlexecute = select @Count=count(UnitID) from....(你的条件)
exec(@str_sqlexecute )

改为
@str_sqlexecute = 'select @Count=count(UnitID) from....'
exec(@str_sqlexecute )

但执行时报错
该怎样写呢?
glboy 2003-02-28
  • 打赏
  • 举报
回复
建议将
Select UnitID From tblUnits Where ParentID In (@SQLCond) And UnitID<>ParentID整个放入一@str_sqlexecute变量中,然后exec(str_sql)

大概:
declare @Count int//作为计数
declare @str_sqlexecute varchar(500)//执行的
@str_sqlexecute = select @Count=count(UnitID) from....(你的条件)
exec(@str_sqlexecute )

if @Count = 0
begin
....
end

else
...
希望能对你有帮助,应该是可以达到你的要求的
angle1219 2003-02-28
  • 打赏
  • 举报
回复
不行
newdongkui 2003-02-28
  • 打赏
  • 举报
回复
不行
你想实现什么
DJMPH 2003-02-28
  • 打赏
  • 举报
回复
应该不行吧,@SQLCond没有执行啊。
动态语句要靠exec来执行的:exec (@SQLCond)
In (@SQLCond)这样恐怕不行。
ldwlqy 2003-02-28
  • 打赏
  • 举报
回复
gaobochina(皮皮)

不行
你试过了吗?
我这结果是
No
joncy88 2003-02-28
  • 打赏
  • 举报
回复
DECLARE @SQLCond varchar(500)

Set @SQLCond='Select UnitID From tblUnits Where ParentID=1234'

set @SQLCond = 'if exists(select UnitID From tblUnits Where ParentID In ' + '(' + @SQLCond + ')'
+ 'and UnitID<>ParentID)' + ' print ' + ''''+'OK' + '''' + ' else print ' + '''' + 'NO' +''''
exec (@SQLCond)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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