Find the schema from database for a table in dbeaver - sql-server

I am using DBeaver 22.0.1 for a client. I have access to test environment. The schema's in test and prod are little different. The client gave me the table name but the schema is different from prod. Is there a way to find the schema of that table using some shortcut.
FYI - I am working for the first time with DBeaver 22.0.1. My ask might sound little silly.

Assuming you are just trying to find the name of the schema for a given table name you can use this query. But please note that if you have the same table name in more than 1 schema you will get all the schemas where this table name exists.
select s.name
from sys.tables t
join sys.schemas s on s.schema_id = t.schema_id
where t.name = 'YourTableNameHere'

Related

Find schema name for a given database?

How do I determine the schema name for a given database "MyDB", listing all tables contained in the database (in MS SQL Express)? Is it "MyDB.Security.Schemas/INFORMATION_SCHEMA"?
I am using EF Core with Blazor, code first.
Background: I want to determine whether a certain table is present in the database.
You can select all the tables with a given and known schema from one database with this query:
SELECT [schemas].[schema_id] AS SchemaId,
[schemas].[name] AS SchemaName,
[tables].[name] AS TableName
FROM sys.schemas AS [schemas]
INNER JOIN sys.tables AS [tables]
ON [schemas].[schema_id] = [tables].[schema_id]
WHERE [schemas].[name] = 'your-schema-name'
If you like to select over all databases on the server you can have a look at this SO answer:
How do I list all tables in all databases in SQL Server in a single result set?

SQL Server Management Studio quickly locate column?

I'm using SSMS and our tables have a lot of columns, so it gets frustrating trying to scroll and try to find a column. Is there a way to quickly locate a column?
You can select from the sys tables
select c.name
from sys.columns c
inner join sys.tables t on t.object_id = c.object_id
where t.name = 'YOURTABLENAME'
and c.name like '%column looking for%'
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

Is Sybase {ASE} uses "schema.table" or just "table"

ALL,
The title pretty much says it all. I was looking here, but couldn't find any information about the schema.
Could someone please sched some light?
I know MS SQL Server uses "schema.name" to reference a table in a database, but for Sybase I'm not sure.
TIA!
EDIT:
I guess I should be more specific.
I know in MS SQL Server it is possible to modify the table owner, but you can still select it with "SELECT su.name FROM sysobjects so, sysusers su, sys.tables t, sys.schemas s WHERE so.uid = su.uid AND t.object_id = so.id AND t.schema_id = s.schema_id AND s.name = ? AND so.name = ?;"
Now with Sybase everywhere I look the query to retrieve the table owner does not reference schema name only the table name. Does this mean that the table owner is not changeable in Sybase? Or it is changeable, but then the schema will also be changed?
Can someone shed some light please?
In Sybase/SAP ASE, all tables have an owner; this owner is effectively the same thing as a schema.
That link you mention points to the sysobjects (system) table, which exists in every database. sysobjects contains some high-level metadata for all objects (eg, tables, procs, triggers, views, etc) in the database.
Two columns of interest in the sysobjects table
name : name of the object (eg, name of a table, name of a proc)
uid : database user id of the object's owner
All system tables have an entry in sysobjects with uid = 1, which refers to the database owner (ie, user_name(1) = dbo).
In most environments it's typical for the database owner (dbo) to also own most other objects in the database (ie, most sysobjects rows have uid=1).
If an object is owned by someone other than the dbo, the sysobjects row will have uid = user_id('non_dbo_owner_name'); for example, if bob's database user id (uid) is 47, then any objects owned by bob will have sysobjects.uid = 47.
When you reference a table without an owner/schema name (eg, select * from tab1), ASE will first look for an object owned by you (ie, do you own a table named 'tab1') and if it doesn't find such a table then it looks for a table owned by the dbo (ie, does 'dbo.tab1' exist?).
When you reference a table with an owner/schema name attached, ASE will only look for the existence of that table.
If you don't own a table named 'tab1' then the following are equivalent:
select * from tab1
select * from dbo.tab1
In response to the updated question:
Sybase/SAP ASE does not support changing the owner of an object.
As for finding the owner(s)/schema(s) of a table, you've got a couple options:
select u.name as 'owner_name',
o.name as 'table_name'
from sysobjects o,
sysusers u
where o.uid = u.uid
and o.name = '<name_of_table>'
and o.type = 'U'
-- or
select user_name(o.uid) as 'owner_name',
o.name as 'table_name'
from sysobjects o
where o.name = '<name_of_table>'
and o.type = 'U'
It works the same like in MS SQL Server. You can use schema.object.
If you use object then the object is searched:
first in our schema
then in dbo schema
You can also use the database.owner.object notation or database..object notation when owner is dbo or event server.database.owner.object notation when you connect an external ASE server.

Retrieve names columns of all tables in a SQL Server database

Does there exist a system stored procedure that lists the names of all tables in a SQL Server 2000 database and per table the names of all columns in that table? I want to export this data to a file for documentation.
I don't know if a system stored procedure exists, but I use this:
SELECT SysObjects.[Name] as TableName,
SysColumns.[Name] as ColumnName,
SysTypes.[Name] As DataType,
SysColumns.[Length] As Length
FROM
SysObjects INNER JOIN SysColumns
ON SysObjects.[Id] = SysColumns.[Id]
INNER JOIN SysTypes
ON SysTypes.[xtype] = SysColumns.[xtype]
WHERE SysObjects.[type] = 'U'
ORDER BY SysObjects.[Name]
In SQL-Server 2005+ you can do it using system views sys.columns and sys.tables
SELECT t.name TableName, c.name ColumnName
FROM sys.tables t
JOIN sys.columns c ON t.object_id=c.object_id
And you can also query the INFORMATION_SCHEMA.COLUMNS view.
It's also safer to use this view. Microsoft says this about the view "Querying the system tables directly may not provide accurate information if system tables are changed in future releases. These views provide an internal, system table-independent view of the SQL Server meta data. Information schema views allow applications to work properly even though significant changes have been made to the system tables."

sql server: looking for table usage through out database

How would I figure out what type of sql code such as procs, functions, views etc. are interacting with my table called TABLE1 through out a given database. Sample code would be very helpful for me.
thanks
select so.name, so.xtype
from sysobjects so (nolock)
inner join syscomments sc (nolock) on sc.id = so.id
where sc.text like '%tablename%'
This code will search all SQL Server objects for a reference to your table. You have to run this query for each database.
If a stored procedure uses your table it will appear in this query. The same is true of functions, views, and triggers.
xtype tells you the type of object.
Here are the possible xtype values:
D = Field names
F = Foreign Key
FN = Function
P = Stored Procedures
PK = Primary Key
S = System Tables
U = User tables
V = Hidden tables
Not enough info in your question, but one thing you can do is use SQL Profiler to profile where INSERTs, UPDATEs, and DELETEs are coming from.
I assume you are talking about how an app is interacting with data and what name (of say a sproc) is doing the insert / update / delete.
Look at SQL Profiler, it comes with your client tools install. Filter it to only show connections to your database (either db name or ID).
If you've been good and created your SPs/views/functions after your table was created, sp_depends will tell you evertyhing referencing the table. Exept for dynamic sql that is.

Resources