I’m trying to select various table and column names from a (SQL Server 2008) database. The database is fairly big. There are several databases sitting on the same server, each with various schemas etc. Once there’s a multi-part identifier involved I’m stuck. For example, to retrieve a list of tables in under a database this works fine:
SELECT [name]
FROM DatabaseOne.sys.tables
ORDER BY [name]
Then I want to get the tables under a certain schema. E.g.:
SELECT [name]
FROM DatabaseOne.SchemaOne.sys.tables
ORDER BY [name]
But I get the error:
Could not find server ‘DatabaseOne’ in sys.servers. Verify that the
correct server name was specified. If necessary, execute the
stored procedure sp_addlinkedserver to add the server to sys.servers.
A large part of the problem probably lies in the fact that I don’t really know anything about schemas (if that’s even what they are).
Also, if I want to find the column names in a table, say DatabaseOne.SchemaOne.TableOne, how would I go about doing that?
Any help would be highly appreciated.
If you use a four part name, SQL Assumes the first portion is the name of a linked server.
The system tables are in the
sysschema, and contain data for all other schemas likedboor whatever.In
sys.tablesthere is aschema_idvalue that specifies which schema each table is in. There is also asys.schemastable which contains the schemas.If you know your schema name, you can do a
You can also check multiple schemas by making the
s.nameevaluation anINevaluation.