create procedure prname
@tn varchar(40)
as
declare @tan varchar(40)
declare @tsql varchar(4000)
set @tan=tn
set @tsql= 'create table '[email protected]+ '(bid int identity(1,1),bname varchar(10),fg int) '
exec(@tsql)
select * from @tan --报错 "Must declare the variable '@tan '. "
为什么不能马上查询 @tan 表??? 或者怎样写? TKS!
------解决方案--------------------
select * from @tan
-----------------
也要动态的执行才可以.
表名在Sql语句中不能使用变量.
如果要用变量就要使用exec来动态的执行语句.
------解决方案--------------------
declare @tan varchar(40)
declare @tsql varchar(4000)
set @[email protected]
set @tsql= 'create table '[email protected]+ '(bid int identity(1,1),bname varchar(10),fg int)
insert '[email protected]+ ' select ' 'yy ' ',34
select * from '[email protected]
exec(@tsql)
------解决方案--------------------
create procedure prname
@tan varchar(40)
as
begin
--declare @tan varchar(40)
declare @tsql varchar(4000)
--set @[email protected]
set @tsql= 'create table '[email protected]+ '(bid int identity(1,1),bname varchar(10),fg int) '
exec(@tsql)
exec( 'select * from '+ @tan )
end
go