问题
I need to find a way to perform the same query to all the tables in an specific database.
For example, if my database DB
has the tables Table1
, Table2
, Table3
, Table4
and Table5
, I want to perform the query
SELECT * FROM <All 5 Tables>
Anyone knows how to do this?
回答1:
USE [DB]
exec sp_msforeachtable 'SELECT * FROM ?'
回答2:
EXEC sp_MSforeachtable N'SELECT * FROM ?'
来源:https://stackoverflow.com/questions/8329661/in-sql-server-i-want-to-use-the-same-query-for-all-the-tables-in-a-database