22,210
社区成员
发帖
与我相关
我的任务
分享
--try
if OBJECT_ID('a')is not null
drop table a
if OBJECT_ID('b')is not null
drop table b
go
create table a ( px int ,学号 int,name nvarchar(10))
create table b ( px int,学号 int,name nvarchar(10))
insert into a select 1, 1,'张三'
union all select 2, 2,'李四'
insert into b select 1,3,'王五'
union all select 2,4,'马六'
declare @str nvarchar(1000)
set @str =' select '
select @str=isnull(@str + ',','')+ b.name+'.'+a.name + ' as ' + b.name + a.name from dbo.syscolumns a join sysobjects b on a.id=b.id and a.id in (OBJECT_ID('a') ,object_id('b'))
set @str = stuff (@str,charindex(',',@str),1,'') +' from a,b'
exec(@str)
(2 行受影响)
apx a学号 aname bpx b学号 bname
----------- ----------- ---------- ----------- ----------- ----------
1 1 张三 1 3 王五
2 2 李四 1 3 王五
1 1 张三 2 4 马六
2 2 李四 2 4 马六
(4 行受影响)
只有给列取别名吧。。
select A.userid as [A.userid],username as [A.username],loginid as[B.loginid],
B.userid as [B.userid],logindate as [B.logindate]
from a join b on a.userid=b.userid