SQL Server Find Out When a Table Was Renamed? - sql

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'

Related

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'

What is the "DATA" keyword in T-SQL for?

I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"

How to find the database name using the table name?

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%';

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%';

Basic SQL Select in Postgresql fails

I am doing a select * on a postgresql table, and everything looks good. But if I do:
SELECT Name from People
It says:
ERROR: column People.Name does not exist
SQL state: 42703
Character: 8
But the name column shows up during select *. I've tried:
SELECT People.Name from People
as well, with the same result. Am I missing something? It should be pretty easy to do this in any other database.
Try putting quotation marks around the column name, i.e. "Name"
PostgreSQL converts everything to lowercase.
If you asks for this:
CREATE TABLE People
{
id SERIAL,
Att1 varchar(100)
-- constraints
};
SELECT Name FROM People;
You should get all the information, because pgsql converts this to
CREATE TABLE people
{
id SERIAL,
att1 varchar(100),
-- constraints
};
SELECT name FROM people;
If you built your table with pgAdmin and created field with mixed casing, you will need to quote them to be sucessfull, like this:
SELECT "Att1" FROM people
Name is a keyword, so it might not be handled well in this case. The best thing to do would be to alias the table like this:
SELECT Name from Peoplep
and then use the p to select the column:
SELECTp.Namefrom People p