这个功能可以这样实现:DECLARE @SQL Nvarchar(5000) DECLARE @int_allid INTSET @SQL=N'SELECT @R=count(BUY.buy_id) as strcount from tbl_post_to_buy BUY'EXEC SP_EXECUTESQL @SQL,N'@R INT OUTPUT',@int_allid OUTPUT
比方说: exec('create table #test(id int) select * from #test drop table #test') 可以。但换成 exec('create table #test(id int)') select * from #test drop table #test 就不行了,不支持!想别的办法吧
每次使用Exec创建的临时表都是不同身份,其他的访问会出问题,好比临时表被释放了。
提示: 连续执行 Exec ('select * into #aaa from t1') 不会报错而 Exec ('select * from #aaa') 或 select * from #aaa Error!Exec('drop table #aaa') ==========================Exec ('select * into ##aaa from t1') ok! Exec ('select * from ##aaa') ok
建议用全局临时表: DECLARE @str_TempTableName varchar(50) set @str_TempTableName='tbl_post_to_buy_searchtemp1111' exec('SELECT count(BUY.buy_id) as strcount into ##' + @str_TempTableName +' from tbl_post_to_buy BUY') exec("select strcount from ##"+@str_TempTableName) exec("drop table ##"+@str_TempTableName) 楼上海兄的方法很好,用sp_executesql。
To Yang_(扬帆破浪) 我用exec建的临时表在后面也就是不在exec中能用吗?
TO liouyun(四海任逍遥) 是的,SQL SERVER 自动清空TEMPTABLE,要用##TEMPTANLE ,不过全局变量要手动DROP TABLE ##TEMPTABLE. 或用 Yang_(扬帆破浪) 的方法。
DECLARE @int_allid INT select @int_allid=strcount from (SELECT count(BUY.buy_id) as strcount from tbl_post_to_buy BUY) a 不需要用临时表
To: Yang_(扬帆破浪) 我用下面的方法不行: DECLARE @str_TempTableName varchar(50) set @str_TempTableName='tbl_searchtemp1111' exec('SELECT count(xx) as strcount into ##' + @str_TempTableName +' from tbl_test') select strcount from ##@str_TempTableName drop table ##@str_TempTableName 但用下面的方法就行: DECLARE @str_TempTableName varchar(50) set @str_TempTableName='tbl_searchtemp1111' exec('SELECT count(xx) as strcount into ##' + @str_TempTableName +' from tbl_test') select strcount from ##tbl_searchtemp1111 drop table ##tbl_searchtemp1111 但是我需要用第一种方法,也就是宏替换的方法. 这是其一,也就是表名是一个传进来的变量,只能用变量替换,不容许直接写表名,因为表名是不定的. 第二,我的SQL语句是动态生成的,必须用EXEC来执行,因为普通的语句必须是什么都明确的情况下可以用的,我的SQL语句根据传进来的参数,用字符串连接的办法生成的,有200个字符以上,而且在EXEC中生成的临时表要在普通的语句中来使用它.说了这么多不知听兄台理解没有,希望帮忙想想办法,万分感激!!
try my sql. 要用动态SQL语句。DECLARE @str_TempTableName varchar(50) set @str_TempTableName='test' exec('SELECT count(qty) as strcount into ##' + @str_TempTableName +' from sales') --print @str_temptableNAme exec('select strcount from ##'+@str_TempTableName) exec ('drop table ##'+@str_TempTableName)
我把海兄的改了一下,用扩展存储过程sp_executesql是最好的。DECLARE @SQL Nvarchar(4000) DECLARE @int_allid INT SET @SQL=N'SELECT @R=count(BUY.buy_id) from tbl_post_to_buy' EXEC SP_EXECUTESQL @SQL,N'@R INT OUTPUT',@int_allid OUTPUT另外,按楼主你的思路,必须要用动态SQL语句,上面好多人都说到了它。
用exec创建的临时表,只能在exec里面的语句里使用。不能在exec之外的语句使用
DECLARE @int_allid INTSET @SQL=N'SELECT @R=count(BUY.buy_id) as strcount from tbl_post_to_buy BUY'EXEC SP_EXECUTESQL @SQL,N'@R INT OUTPUT',@int_allid OUTPUT
exec('create table #test(id int) select * from #test drop table #test')
可以。但换成
exec('create table #test(id int)')
select * from #test
drop table #test
就不行了,不支持!想别的办法吧
提示:
连续执行 Exec ('select * into #aaa from t1') 不会报错而
Exec ('select * from #aaa') 或 select * from #aaa
Error!Exec('drop table #aaa')
==========================Exec ('select * into ##aaa from t1')
ok!
Exec ('select * from ##aaa')
ok
DECLARE @str_TempTableName varchar(50)
set @str_TempTableName='tbl_post_to_buy_searchtemp1111'
exec('SELECT count(BUY.buy_id) as strcount into ##' + @str_TempTableName +' from tbl_post_to_buy BUY')
exec("select strcount from ##"+@str_TempTableName)
exec("drop table ##"+@str_TempTableName)
楼上海兄的方法很好,用sp_executesql。
我用exec建的临时表在后面也就是不在exec中能用吗?
是的,SQL SERVER 自动清空TEMPTABLE,要用##TEMPTANLE ,不过全局变量要手动DROP TABLE ##TEMPTABLE.
或用 Yang_(扬帆破浪) 的方法。
两个exec对数据库来说是两个不同的连接,所以用局部临时表不行,用全局临时表示可以的。
用SP_EXECUTESQL 能解决你的问题。
select @int_allid=strcount from
(SELECT count(BUY.buy_id) as strcount
from tbl_post_to_buy BUY) a
不需要用临时表
我用下面的方法不行:
DECLARE @str_TempTableName varchar(50)
set @str_TempTableName='tbl_searchtemp1111'
exec('SELECT count(xx) as strcount into ##' + @str_TempTableName +' from tbl_test')
select strcount from ##@str_TempTableName
drop table ##@str_TempTableName
但用下面的方法就行:
DECLARE @str_TempTableName varchar(50)
set @str_TempTableName='tbl_searchtemp1111'
exec('SELECT count(xx) as strcount into ##' + @str_TempTableName +' from tbl_test')
select strcount from ##tbl_searchtemp1111
drop table ##tbl_searchtemp1111 但是我需要用第一种方法,也就是宏替换的方法.
这是其一,也就是表名是一个传进来的变量,只能用变量替换,不容许直接写表名,因为表名是不定的.
第二,我的SQL语句是动态生成的,必须用EXEC来执行,因为普通的语句必须是什么都明确的情况下可以用的,我的SQL语句根据传进来的参数,用字符串连接的办法生成的,有200个字符以上,而且在EXEC中生成的临时表要在普通的语句中来使用它.说了这么多不知听兄台理解没有,希望帮忙想想办法,万分感激!!
要用动态SQL语句。DECLARE @str_TempTableName varchar(50)
set @str_TempTableName='test'
exec('SELECT count(qty) as strcount into ##' + @str_TempTableName +' from sales')
--print @str_temptableNAme
exec('select strcount from ##'+@str_TempTableName)
exec ('drop table ##'+@str_TempTableName)
DECLARE @int_allid INT
SET @SQL=N'SELECT @R=count(BUY.buy_id) from tbl_post_to_buy'
EXEC SP_EXECUTESQL @SQL,N'@R INT OUTPUT',@int_allid OUTPUT另外,按楼主你的思路,必须要用动态SQL语句,上面好多人都说到了它。