多问一句,如果tablename有10甚至更多一些,如上这样查询:
select id,(select name from b where id=a.id) from a where tablename='b'
union
select id,(select name from c where id=a.id) from a where tablename='c'
union
select id,(select name from d where id=a.id) from a where tablename='d'
union
select id,(select name from d where id=a.id) from a where tablename='e'
union
select id,(select name from d where id=a.id) from a where tablename='f'
union
select id,(select name from d where id=a.id) from a where tablename='g'
union
select id,(select name from d where id=a.id) from a where tablename='h'
select id,(select name from b where id=a.id) from a where tablename='b'
union
select id,(select name from c where id=a.id) from a where tablename='c'
union
select id,(select name from d where id=a.id) from a where tablename='d'
select a.id,b.name from a,b where a.tablename='b' and a.id=b.id
union all
select a.id,c.name from a,c where a.tablename='c' and a.id=c.id
union all
select a.id,d.name from a,d where a.tablename='d' and a.id=d.id
select id,(select name from b where id=a.id) from a where tablename='b'
union all
select id,(select name from c where id=a.id) from a where tablename='c'
union all
select id,(select name from d where id=a.id) from a where tablename='d'
create procdure pd_name
as
declare @id int
declare @tablename varchar(50)
select @id=id,@tablename=tablename from A where 条件
exec ('select * from '+@tablename+' where id='+@id)
DECLARE tables_cursor CURSOR
FOR
SELECT name FROM sysobjects WHERE type = 'U'
OPEN tables_cursor
DECLARE @tablename sysname
FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@FETCH_STATUS <> -1)
BEGIN
/* A @@FETCH_STATUS of -2 means that the row has been deleted.
There is no need to test for this because this loop drops all
user-defined tables. */.
EXEC ('DROP TABLE ' + @tablename)
FETCH NEXT FROM tables_cursor INTO @tablename
END
PRINT 'All user-defined tables have been dropped from the database.'
DEALLOCATE tables_cursor