In the code below, I am able to specify the table name and schema that I want to query with Table name and Table schema. How do I specify the database name and server name that I want to query?
I have tried DATABASE_NAME, DB_NAME and SERVER_NAME. None of them work.
SELECT COUNT(1) AS TABLECOUNT FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_SCHEMA = 'dbo' AND TABLE_NAME = '[TABLE_NAME]
Here is how you can select database, table and server-name :
SELECT COUNT(1) AS TABLECOUNT, @@SERVERNAME as 'serverName' FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'CALCULATION_SCHEDULE'
and @@SERVERNAME ='xx' and TABLE_CATALOG ='database name'
click here to know more about the serverName
You need to use TABLE_CATALOG
for passing database name. In SQL Server this column contains the database name that contains that table as shown below.
SELECT COUNT(1) AS TABLECOUNT FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'CALCULATION_SCHEDULE'
AND TABLE_CATALOG = '<Your Database Name>'
You can also pass directly server name and database name as shown below.
SELECT * FROM [ServerName].[DatabaseName].INFORMATION_SCHEMA.TABLES where ---.
There is no table or view that enumerates all tablesof all databases, you have to explicitly specify database:
Directly in query
SELECT COUNT(1) AS TABLECOUNT FROM [dbname].INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'CALCULATION_SCHEDULE' ;
Via "USE"
USE [dbname] SELECT COUNT(1) AS TABLECOUNT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'CALCULATION_SCHEDULE' ;
In your connection string
Regarding a Server Name, it is possible only via system variables like @@SERVERNAME or SERVERPROPERTY('ServerName'):
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'CALCULATION_SCHEDULE'
AND SERVERPROPERTY('ServerName') = 'YourServer' ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.