请问:在mssql中用SELECT语句做表名或者列名的语法是什么样的? select username from (select name from [table] where id=1) new where uid=2 这句语法是对得。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select a.[name] from (select [name],uid from 表名 where [id]=1) as a where a.uid=2 declare @表明 varchar(100)set @表明='xxxxxx'exec('select * from '+@表明) select name from (select * from table where id=1) new where id=2 ---?????? 要用别名,而且用的字段扼也要加。select 和where后的字段一定要在结果集中 select username from (select name ,uid from [table] where id=1) new where uid=2 username 当然不行了,要用name ,你要选择的列username在子查询吗???select name from (select * from table where id=1) as new where id=2 我曾看到过一篇把oracle和mssql比较的文章,那里边提到可以用子查询的结果来作表名或者列名,但是写的很笼统,没看懂 :( select username from (select name as username,id as uid from table where id=1) as new where uid=2 你在外面用到的字段名一定要在括号中的SELECT中有这个列名. declare @表明 varchar(100)set @表明='xxxxxx'exec('select * from '+@表名)这个是可以的,@表名这个变量可以用游标,循环赋值的但是如果你想做exec('select count(*) from '+@表名)中取得 count(*) ,这样是无能为力的!我今天下午也正在想这个问题 declare @strSQL varchar(500) select @strSQL =name from table where id=1 SET @strSQL ='select username from '+@strSQL+' where uid=2 ' EXEC(@strSQL) 我曾看到过一篇把oracle和mssql比较的文章,那里边提到可以用子查询的结果来作表名或者列名,但是写的很笼统,没看懂 re:你说的是子查询:select * from (select * from 表1) 别名 隔五个取一个数据 排序时能否按特定的词排啊?? [等待中],SQL的企业管理器突然就打不开了,提示:MMC不能打开文件...... sql的复制除了要用到1433端口外还需要什么端口? 简单问题,急 sql语句 sql 约束规则定义 高手(还是季度问题 上次的没表达清楚 不好意思) 没解决好,再开一帖,请高手解决啊 如何在DB2中创建存储过程 SQL 循环问题请教,请进!谢谢! 怎么把存储过程中的返回值放到数据库里?
where a.uid=2
set @表明='xxxxxx'
exec('select * from '+@表明)
where id=2
---
??????
select 和where后的字段一定要在结果集中
where uid=2
where id=2
where uid=2 你在外面用到的字段名一定要在括号中的SELECT中有这个列名.
set @表明='xxxxxx'
exec('select * from '+@表名)
这个是可以的,
@表名这个变量可以用游标,循环赋值的
但是
如果你想做
exec('select count(*) from '+@表名)
中取得 count(*) ,这样是无能为力的!
我今天下午也正在想这个问题
SET @strSQL ='select username from '+@strSQL+' where uid=2 '
EXEC(@strSQL)