I created a new schema in SQL Server using
CREATE SCHEMA testschema
I checked database_principals table using
select * from mssql.sys.database_principals;
I did not find any entry for testschema.
Did I miss something?
I am able to access tables in the dbo schema. I want to create testschema with same privilege as dbo.
Connection String for JDBC :
jdbc:sqlserver://192.xxx.xxx.xxx:1433;databaseName=BCHN
IIRC up to SQL Server 2000 schemas and principals were not distinguished. If you work on 2005 or higher, the new schema should display with the statement
select * from sys.schemas
CREATE SCHEMA does not create database principal.
SELECT * FROM sys.schemas
WHERE
name = 'testschema'
As you can see: principal_id = 1 (dbo)
It must be possible to access objects in your schema with [schema_name].[object_name]
Related
I am trying to read data from DB1, do some calculations on it and then store it in DB2. Both DB1 and DB2 are on the same server.
However, when I connect to the server on SSMS, it does not show me DB1 and DB2 under the same connection. How do I connect to the server such that it shows both DB1 and DB2 in the same connection?
A connection can have only one database context. However, you can use 3-part names to qualify object names with other database names, allowing a query to use objects in other databases on the same connection.
USE Database1; --use Database1 as default context
SELECT
'Database1' AS DatabaseName
, OBJECT_SCHEMA_NAME(object_id) AS SchemaName
, name AS TableName
FROM sys.tables -- 1 or 2-part name uses current database
UNION ALL
SELECT
'Database2' AS DatabaseName
, OBJECT_SCHEMA_NAME(object_id) AS SchemaName
, name AS TableName
FROM Database2.sys.tables; -- 3-part name uses other database
Also, 4-part names allow one to use objects on other servers via linked servers.
Thanks for the help Dan - the 4-part naming did not work for me. Ended up creating an external table in DB2 which allowed me to query data stored in DB1
In Sql Management Studio (2012) I have set up a linked Oracle server which works fine. However, I cannot see or access any tables that are in the default schema (i.e., which do not have a schema name).
In other words I can do this:
SELECT * FROM (LinkName)..(SchemaName).(TableName)
but I can't do this:
SELECT * FROM (LinkName)...(TableName)
These tables also do not show up under the Linked Server tables folder, and doing a select returns the error "The table either does not exist or the current user does not have permissions on that table."
I know the tables are there and I have the appropriate rights because I can see and access these tables when working with SSIS on the same machine, and with the same credentials.
Any ideas?
I have a view in SQL Server, lets say MY_VIEW.
When I execute the command SELECT * FROM MY_VIEW it works fine. However, when I execute it as
SELECT * FROM dbo.MY_VIEW I get *Invalid object name 'MyDB.dbo.MY_VIEW'*
I am connected to SQL server using sa.
What is wrong with this? And when should we use dbo.MY_VIEW and when not?
Update: The schema name on the view is dbo and when I created the view then too I had connected with sa.
Update2 I found the problem was case sensitive collation. The problem was not because of the dbo. prefix. It was because the database collation was case sensitive and table names in queries were in wrong case.
Did you create your objects under a different schema name than dbo? It would depend on the default schema name for your user account if you didn't qualify it when you created the view. In SQL 2k5 and 2k8 I believe the default behavior is to create a new schema for each user vs. assigning them to the 'dbo' schema.
You are in the master database. You created the view in the master database. Your actual query was SELECT * FROM MyDB.dbo.MY_VIEW. Try creating the view in the MyDB database instead.
I found the problem was case sensitive collation. The problem was not because of the dbo. prefix. It was because the database collation was case sensitive and table names in queries were in wrong case.
I have a schema define in my database. Except now everytime I do a sql statement I have to provide the schema ...
SELECT * FROM [myschema].table
I set the default schema for my user using management studio and also ran the
ALTER USER myUser WITH DEFAULT_SCHEMA [myschema]
and I still get the invalid object 'table' when writing a query without the schema (SELECT * FROM table)
Is there a way to write SELECT * FROM table without having to specify the schema name all the time?
It's on SQL 2005 using SQL Management Studio.
Is the user an SA, if so it will not work, according to the documentation SA users are always defaulted to the dbo schema.
The value of DEFAULT_SCHEMA is ignored
if the user is a member of the
sysadmin fixed server role. All
members of the sysadmin fixed server
role have a default schema of dbo.
Couple of options:
Is your user listed under Security > Users (in SSMS)? Check the Properties (right click the name), and see if the Default schema is set in the context of the database, rather than the instance (which is what ALTER USER is setting).
Create a synonym for the table you want to reference:
CREATE SYNONYM table_name
FOR [your_db].[your_schema].table_name
...which will affect everyone who doesn't use at least two name notation, in the context of that database. Read more about it here. But it is associated ultimately to a schema.
Check that the database selected in the "Available Databases" drop down (upper left, to the left of the Execute button) is correct.
Use three name notation when specifying table (and view) references:
SELECT *
FROM [your_db].[your_schema].table_name
If you do not want to use "full qualified" SQl names, then you need to avoid creating your tables using any account or role that's not using the "dbo" default schema assigned. Why do you need to change the default schema on the user if you don't plan on using it?
I am trying to drop one of the role in my SQL Server database. I dropped all the members from the role and when i tried to drop role i got this error message:
Msg 15138, Level 16, State 1, Line 13
The database principal owns a schema in the database, and cannot be dropped.
Does anyone know why? I checked the Owned Schema and it only had check sign in its own name.
You cannot drop a database principal that owns a schema. You have to transfer the schema ownership to some other database principal or drop the schema before you can drop the database principal.
15138 error is due to the user you are trying to delete owns a schema.
If you run the below query you will get the schema owned by the user.
USE DatabaseName;
SELECT s.name
FROM sys.schemas s
WHERE s.principal_id = USER_ID('UserName');
Let us say it returns 'db_denydatareader' schema. Then you can assign
that schema to default user 'dbo' using the below query.
ALTER AUTHORIZATION ON SCHEMA::db_denydatareader TO dbo;