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

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.

Related

Find the schema from database for a table in dbeaver

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'

List of all different database tables used in views

I need to find all the different database tables used in views.
I tried information_schema.view_table_usage.
I got results for current database.
But tables associated with different database are not fetching.
Please guide.
You can use the sys tables sys.objects and sys.sysdepends to retrieve the information you as follows
--Replace 'YOU VIEW NAME' by the view you want
--1) using old sys tables
select distinct(ov.name),ov.xtype from sysobjects o
inner join sysdepends d on o.id=d.id
inner join sysobjects ov on d.depid=ov.id
where o.name='YOUR VIEW NAME'
--2) using new sys.table
select distinct(ov.name),ov.type from sys.objects o
inner join sys.sysdepends d on o.object_id=d.id
inner join sys.objects ov on d.depid=ov.object_id
where o.name='YOUR VIEW NAME'
Don't use information_schema (here's why) or sysdepends (here's why).
SELECT v.name,
ed.referenced_database_name,
ed.referenced_schema_name,
ed.referenced_entity_name
FROM sys.views AS v
INNER JOIN sys.sql_expression_dependencies AS ed
ON v.[object_id] = ed.referencing_id;
If you only want things in another database, add:
WHERE ed.referenced_database_name IS NOT NULL;
You have to run this query from the context of the database where the view exists.
As an aside, permissions are a little different. From the documentation:
Requires VIEW DEFINITION permission on the database and SELECT permission on sys.sql_expression_dependencies for the database.
If you don't have these permissions (explicitly, or though db_owner/sysadmin or a few other roles), you won't get an error message, the view will just return no rows.
INFORMATION_SCHEMA.VIEW_COLUMN_USAGE, on the other hand, has a little more freedom, but still has the limitation you observed (only returns references within the same database):
Returns one row for each column in the current database that is used in a view definition. This information schema view returns information about the objects to which the current user has permissions.
In order to use sys.sql_expression_dependencies, you'll need someone to grant you the appropriate permissions, or they'll need to create a stored procedure that calls it, executes as owner, and give you permissions to call that stored procedure.

Min SQL user privilege to query sys.objects and sys.partitions in Azure SQL

I would like to have SQL user which can perform following SQL, but not able to read contents of other schemas.
What is best way to implement?
SELECT (Schema_name(A.schema_id) + '.' + A.NAME ) AS TableName,
Sum(B.rows) AS RecordCount
FROM sys.objects A
INNER JOIN sys.partitions B
ON A.object_id = B.object_id
WHERE A.type = 'U'
GROUP BY A.schema_id, A.NAME
For columns, there is a row for every permission that is different from the corresponding object-level permission. If the column permission is the same as the corresponding object permission, there is no row for it and the permission applied is that of the object.
Note: Column-level permissions override object-level permissions on
the same entity.
Specifies a permission that can be granted on a schema. Here.
In rare circumstances, combining ALTER and REFERENCE rights may allow the grantee to access data or perform prohibited operations. For instance, a user with ALTER access on a table and REFERENCE permission on a function can build and execute a calculated column over a function. The user must also have SELECT permission on the calculated column in this situation.

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