SQL中的动态语句执行--exec(@sqlstr)

 1 begin
 2 drop table #tmptable
 3 declare @money ut_money
 4 set @money=1.2345
 5 create table #tmptable
 6 (
 7 je ut_money
 8 )
 9 
10 insert into #tmptable(je) values(@money)
11 
12 select * from   #tmptable
13 
14 end

执行结果:

 

第二段sql

 1 begin
 2 drop table #tmptable
 3 declare @money ut_money
 4 set @money=1.2345
 5 create table #tmptable
 6 (
 7 je ut_money
 8 )
 9 
10 exec('insert into #tmptable(je) values('+@money+')')
11 
12 
13 select convert(varchar(20),@money)
14 select * from   #tmptable
15 
16 end

执行结果:

 

 

以上结果表明:

exec(@sqlstr)

其中@sqlstr中如果包含变量的运算,是将变量转换为varchar后再exec操作

posted @ 2017-12-12 10:29  有为青年爱有为  阅读(2273)  评论(0编辑  收藏  举报