--创建一个表 CREATE TABLE [100] (id int) INSERT INTO [100] SELECT 1 --根据变量查询表 DECLARE @tt INT SET @tt=100DECLARE @sql VARCHAR(200) SET @sql='select * from ['+CAST(@tt AS VARCHAR(200))+']' EXEC(@sql) /* 1 */
declare @tt int set @tt = 100declare @sql varchar(1000) set @sql = 'if object_id('''+ltrim(@tt)+''') is not null select * from ['+ltrim(@tt) +'] else begin CREATE TABLE ['+ltrim(@tt)+'] (id int) INSERT INTO ['+ltrim(@tt)+'] SELECT 1 select * from ['+ltrim(@tt)+'] end ' exec(@sql)
declare @tt int declare @sql varchar(8000) set @tt=100 set @sql='select * from ' + @ttexec(@sql)
--创建一个表
CREATE TABLE [100] (id int)
INSERT INTO [100] SELECT 1
--根据变量查询表
DECLARE @tt INT
SET @tt=100DECLARE @sql VARCHAR(200)
SET @sql='select * from ['+CAST(@tt AS VARCHAR(200))+']'
EXEC(@sql)
/*
1
*/
set @tt = 100declare @sql varchar(1000)
set @sql = 'if object_id('''+ltrim(@tt)+''') is not null select * from ['+ltrim(@tt)
+'] else begin
CREATE TABLE ['+ltrim(@tt)+'] (id int)
INSERT INTO ['+ltrim(@tt)+'] SELECT 1
select * from ['+ltrim(@tt)+'] end
'
exec(@sql)
declare @sql varchar(8000)
set @tt=100
set @sql='select * from ' + @ttexec(@sql)