How to find the database name using the table name? - sql-server-2012

I found the table name exist in my system, I used the following query;
select * from sys.tables where name = '%openpotab%';
But how can I find the database name using the table name?
thanks in advance,
Bhavesh

You could use:
select DB_NAME(DB_ID()), * from sys.tables where name LIKE '%openpotab%';

Related

Gather all column names into a table

I am working on a project whos purpose is to rename all table names and column names in the sql database from one language to another. I have gather all the local table names into a table ltbl_TableNames and want to add all the columns of these tables into a table called ltbl_TableColumns.
I also want every table column to have a link to their table name. For example, the table 'Sales' have a column named 'Sum'. The table 'Sales' has the ID '10000'. I want to add that ID in a column named 'TableName_ID' for linking purposes. Is this possible to do without a lot of hassle?
Disclaimer: I am thinking about the renaming process. I only want to gather the column names with link to their parent table name.
Thanks in advance for answers.
You may use sys.tables for getting list of table name in your database.
Similarly for column name you may use information_schema.columns as this will give records with table name.
From the above 2 records you can easily make you required result.
;WITH CTABLE AS
( SELECT * FROM SYS.TABLES WHERE TYPE_DESC = 'USER_TABLE' )
, COLUMNNAME AS
( SELECT * FROM INFORMATION_SCHEMA.COLUMNS )
SELECT * INTO NEWTABLE FROM
(SELECT CTABLE.NAME AS TABLENAME , COLUMNNAME.COLUMN_NAME, COLUMNNAME.COLUMN_NAME + '_' + CAST(CTABLE.OBJECT_ID AS VARCHAR(15)) AS NEW_COLUMNNAME
FROM CTABLE INNER JOIN COLUMNNAME ON CTABLE.NAME = COLUMNNAME.TABLE_NAME ) AS D
You may try this for your result.

DSN8a10.emp is an undefined name

I just created a table named TELE by running the following query:
CREATE TABLE TELE
(NAME2 VARCHAR(15) NOT NULL,
NAME1 VARCHAR(12) NOT NULL,
PHONE CHAR(4));
Now, I am trying to populate it with data from the DSN8A10.EMP table, by running the following query:
INSERT INTO TELE
SELECT LASTNAME, FIRSTNME, PHONENO
FROM DSN8A10.EMP
WHERE WORKDEPT = 'D21';
But I get the following error:
[42704][-204] "DSN8A10.EMP" is an undefined name.. SQLCODE=-204,
SQLSTATE=42704, DRIVER=4.23.42.
I am using IntelliJ IDEA with com.ibm.db2.jcc.DB2Driver Data Server Driver.
Can you help me with a solution, please?
Thanks in advance!
Some possibilities:
the table does not exist because you have a typo in the schema name or the table name
the table does exist but in a different database
the table exists in the database, but the name or schema has a MiXed case, in which case you must use double quotes around the schema name and table name. So "DSN8a10"."emp" is DIFFERENT from DSN8a10.EMP.
If the Db2-server runs on Linux/Unix/Windows, this query may help to show a mixed case name. It's possible the table is a view or alias or nickname.
select tabschema, tabname from syscat.tables where upper(tabschema)='DSN8A10' and upper(tabname) = 'EMP'
If the Db2-server runs on i-Series: use QSYS2.SYSTABLES instead.
select table_schema, table_name from qsys2.systables where upper(table_schema)='DSN8A10' and table_name='EMP'
If the Db2-server runs on Z/OS: use SYSIBM.SYSTABLES instead:
select creator, name from sysibm.systables where upper(creator)='DSN8A10' and upper(name) = 'EMP'

Get SQL Server database created Information

I have created a database in SQL Server 2012 with name "yadav". Now I want to know where "Yadav" DB info is stored like Created Date, Number of Tables, User Name etc.
Thanks in advance.
Some basic things are accessible in the sys.databases catalog view:
SELECT name, database_id, create_date
FROM sys.databases
WHERE name = 'yadav'
For other, you'll have to go to your database:
USE yadav;
SELECT TableCount = COUNT(*)
FROM sys.tables
and there's a vast collection of other system catalog views which provide insight into your database and its objects
You need something like this
sp_helpdb 'Yadav'
You can get the database information from sys.databases table.
select *
from sys.databases
where name = database_name
and table information from INFORMATION_SCHEMA.COLUMNS table.
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG = database_name

SQL Server Find Out When a Table Was Renamed?

Is it possible to find out when a table was renamed? (as a primary question)
And if possible which user did it?
You can try this:
SELECT Name, modify_date, * FROM sys.all_objects
WHERE Name = 'Table name'

How can I Retrieve a List of All Table Names from a SQL Server CE database?

Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';