I use sql-server-2005,My database contains many tables.
When I explore my db, I see all table names are prefixed with "dbo". I want to replace the "dbo" with the database name see below:
dbo is a schema name, it has nothing to do with database name. SQL server uses 4 part names : [ServerName].[Database name].[Schema name].[Table/View/Trigger/SP/Function/etc Name]. Default schema is dbo, but you can name it after database if you want (see http://msdn.microsoft.com/en-us/library/ms189462.aspx for details)
As other have said dbo is the default schema. I'm not sure why you would want to create a schema that has the same name as the database, but to accomplish what you've asked you would have to do something like the following:
Create a new schema using the CREATE SCHEMA statement
Move your tables from the default schema to the new schema using the ALTER SCHEMA statement.
An example would be:
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name ='iMature_US')
EXEC dbo.sp_executesql #statement=N'CREATE SCHEMA iMature_US';
GO
ALTER SCHEMA iMature_US TRANSFER dbo.PODetail
GO
You could write a script to transfer all the tables using data from the INFORMATION_SCHEMA.TABLES view.
Related
A little challenge for the MS Sql Server experts out there...
I have a database on a SQL Server 2008 R2 server.
I have created a schema named MPP and a handful of table for this schema.
The tables were originally created in the default dbo schema and then moved to the new schema using the sp_changeobjectowner procedure. The tables were however created and dropped a few times before I got it to work as I wanted.
To the problem... Whenever I now create a new table, regardless in the default dbo schema or I get one error message per table in the new schema referring to an error in the sp_changeobjectowner procedure:
CREATE TABLE dbo.test (d INT);
Msg 15001, Level 16, State 1, Procedure sp_changeobjectowner, Line 64
Object 'ASBJOR.TEST_TABELL' does not exist or is not a valid object for this operation.
There is a database user named ASBJOR, but no schema.
If I add a new table to the MPP schema the number of error messages will increase and one of the messages refers to the new table.
I've tried to create a new table in the default dbo schema and move it to the MPP schema using the ALTER SCHEMA MPP TRANSFER... command, but new table still appears in the error messages.
I've looked through the sys.objects, sys.tables, sys.schemas and sys.database_principals tables but I can't find anything wrong.
Any ideas what could cause this error message or where I can look for more hardcore details in the database?
You should never use sp_changeobjectowner - it's been deprecated since 2005. Always use ALTER SCHEMA ... TRANSFER.
As for the error, check for a DDL trigger in that database:
SELECT * FROM sys.triggers WHERE parent_class = 0;
Then check to see what those triggers are doing.
I'm facing this problem, I created a database with multiple schemas other than public. When I connect with postgres user role (using pgAdmin, if I execute a create table statement, the table is not created but if I browse information_schema.tables that table is listed as table_schema=information_schema, even if in create statement I qualify the table with public schema. After that, if I create another table that reference the first one or just select from that table, postgres complains that the relation does not exixts. Then I have to drop the table to procede anyway.
If I connect with another user role, that have superuser property set, the table is created in public schema.
Is there any database property that I have to set or something other I have to do to avoid the above problem?
Please try creating tables with mentioning schema name like (create table schemaname.tablename(...)) may be it solve your problem.
May be you type
set search_path = information_schema;
to make some queries in the information_schema. But when you create the table, it was created in the information_schema. That was because the first schema name in set search_path is the default for creating objects.
I have to 2 databases DB1 and DB2.
I have a view called View1 which is stored in the database DB2. Now I want to ALTER VIEW from DB1 database.
My attempt:
ALTER VIEW DB2..View1
AS
SELECT * FROM DB2..Test;
But I'm getting an error:
'CREATE/ALTER VIEW' does not allow specifying the database name as a prefix to the object name
It is really simple: you need to change the database
USE DB2
GO
ALTER VIEW View1
...
In my case I'm trying to run a script to create/alter a view in different database, so I'm using EXEC (...) to create my views.. But I hit a bit of a paradox:
EXEC ('CREATE VIEW...') will not let you specify the database. You have to switch to that database to create the view.
But you can't do EXEC ('USE [db]; CREATE VIEW...') as CREATE VIEW will demand it be the first command.
I got around this problem feeling like I went Inception:
EXEC('USE [db]; EXEC('CREATE VIEW'))
Comments are self explanatory. You need to be working in the DataBase your view is for/from. Switch your connection to DB2 and you should be able to CREATE and ALTER a/your view. From MSDN
I was connected to the same database server:DB1 in Azure Data Studio where I wanted the view, and getting the error from this:
CREATE OR ALTER VIEW DB1.dbo.my_View AS
SELECT ...
I removed the prefix and this worked:
CREATE OR ALTER VIEW my_View AS
SELECT ...
i am connecting to sql server with user name as sa and Password=Password()345
i have a database named Demo , when i creating any database objects like tables or procedures it is placed in dbo. but i want to put in a schema like ABCD . how to do this . Can anyone tell me the steps . i am new to sql server
First you might want to create a schema:
CREATE SCHEMA somename;
GO
Then you create a table like this:
CREATE TABLE somename.TestTable (col1 int);
SQL Server lets you even do it in a single step:
CREATE SCHEMA somename AUTHORIZATION sa
CREATE TABLE tab1 (col1 INT);
GO
It's as simple as:
CREATE SCHEMA NewSchema;
CREATE TABLE NewSchema.NewTable
(
....
);
CREATE PROC NewSchema.NewProc
AS ....;
Same as you would do for dbo.
You might also need to consider granting applicable access
To move a user's default schema to this one:
ALTER USER userName
WITH DEFAULT_SCHEMA = NewSchema;
CREATE SCHEMA ABCD
GO
CREATE TABLE ABCD.TableName (...)
How do I get away with hardcoding the database name in referencing a table within a stored procedure. For example there are two databases db1 and db2. I am writing a stored procedure in db2 which references two tables, one from db1 and another from db2. Both are on the same sybase server.
If I understand your question correctly, on the one hand, in your stored procedure you can refer to the table in the same database directly by name
SELECT ...
FROM table_in_db2
You can refer to a table in database db1 by prefixing the database name and an empty owner:
SELECT ...
FROM db1..table_in_db1
On the other hand, if you want to avoid hard-coding database names in the procedure you might create a view in database db2 that references the db1 table:
CREATE VIEW view_in_db2
AS
SELECT *
FROM db1..table_in_db1
and use that in the procedure:
SELECT ...
FROM view_in_db2
You need to keep the code portable, involve 2 databases, but avoid referencing databases by name. Then you can create proxy tables (or proxy views, if such views exist in 12.5). Refer to proxy tables as to local objects.
This will work, but will require some extra care, every time you move/change databases. But anyway the separation of concerns you are after can be achieved.