Drop table name same as Database name - sql-server

Currently by mistake I created a table with the same name as a Database name in SQL server. Now I want to drop the table. How should I do that?
table name: dbo.employee
database name: dbo

Let your table name be same as DB name; that won't cause any issue cause to drop a table you use drop table ... command and to drop database drop database ... command. BTW, from your post that dbo rather looks like schema name.

Related

view created table in microsoft sql server

I am running part of a query in Microsoft SQL server management studio
Select Table1.Column1
into #Table2
from Table1
now it has created the table but I actually want to view this table with my eyes but I cannot seem to find where the table is stored. Please could someone help me find it?
That is a temporary table. It will be created in the tempdb system database and you can see it by going to tempdb -> Temporary Tables.
Any tables where its name start with # is a Temporary Table. Exactly as the name suggests, it's temporary, and only exists for the same time the connection that created it does (or it is dropped).
If you want to view the data from a temporary table, you would do so like any other table SELECT * FROM #Table2;. .
I imagine what your really after is to not use a temporary table, so drop the # from the name, and the new table will be created in the database you are connected to.

SQL server: To create a table inside a schema

I wish to create a table T1. And when I execute the query that table should be include in HumanResources schema which already exists in the database.
How should I change my query to do this? To get table T1 into the HumanResources schema?
Create Table T1
(
Id int,
Name varchar(20)
)
Create Table HumanResources.T1 (...);
In your attempt, you are trying to add it to a database called HumanResources and to the schema dbo. It's database.schema.object.
Edit
In response to the OP's comment, the question has already been answered here: How do I create a SQL table under a different schema?
The schema that will be used when schema is omitted will be the
default schema of the database user. Therefore, for creating table
without specifying schema, you'd have to set that database user's
default schema to dbo.
In your case try running:
CREATE TABLE [schemaname].[tableName](...)

Remove record from sys.objects?

I ran a ALTER SCHEMA ... on a table (SQL Server 2012 SP1) but the sys.objects record is still there. When I run the ETL I get an error that the table doesn't exist anymore because it's trying to remove all constraints. This one is of type PK Primary Key Constraint. How can I safely remove the record from the sys.objects table?
I managed to fix this issue by scripting the database to a new one, ALTER SCHEMA ... again on that table to bring it back to dbo., then DROP and recreate the table in the different table schema.

SQL Compare deployment scripts do not include all ALTER SCHEMA commands

I have a database that I am trying to clean up on SQL Server 2008 (not R2). Currently, all tables reside in the dbo schema. Some table names are singular, others are plural.
I created a new schema, crm. I moved all of the tables from dbo to crm and I renamed the singular table names to match the plural table names. When I perform the SQL Compare (version 10.4.8.87) between my development database and production, the script includes the following:
PRINT N'Creating schemata'
GO
CREATE SCHEMA [crm]
AUTHORIZATION [dbo]
GO
...
(removes foreign key constraints, notice it removes them from the plural tables in dbo schema)
PRINT N'Dropping foreign keys from [dbo].[CustomersCommittees]'
GO
ALTER TABLE [dbo].[CustomersCommittees] DROP CONSTRAINT [FK_CustCom_ComID]
ALTER TABLE [dbo].[CustomersCommittees] DROP CONSTRAINT [FK_CustCom_CustID]
GO
...
EXEC sp_rename N'[dbo].[Customer]',N'Customers',N'OBJECT'
ALTER SCHEMA [crm] TRANSFER [dbo].[Customers]
EXEC sp_rename N'[dbo].[CustomerAddress]',N'Addresses',N'OBJECT'
ALTER SCHEMA [crm] TRANSFER [dbo].[Addresses]
EXEC sp_rename N'[dbo].[Committee]',N'Committees',N'OBJECT'
ALTER SCHEMA [crm] TRANSFER [dbo].[Committees]
...
(adds foreign key contraints back, notice how it adds them to the plural tables in the new crm schema, but never included a statement to ALTER SCHEMA)
PRINT N'Adding foreign keys to [crm].[CustomersCommittees]'
GO
ALTER TABLE [crm].[CustomersCommittees] ADD CONSTRAINT [FK_CustCom_ComID] FOREIGN KEY ([CommitteeID]) REFERENCES [reference].[Committees] ([CommitteeID])
ALTER TABLE [crm].[CustomersCommittees] ADD CONSTRAINT [FK_CustCom_CustID] FOREIGN KEY ([CustomerID]) REFERENCES [crm].[Customers] ([CustomerID])
GO
...
As mentioned above, it does not include any of the ALTER SCHEMA ... TRANSFER ... commands for the tables that were already plural and do not need the sp_rename command to be executed.
Has anyone else seen this?
Maybe I could answer more accurately if I know how SQL Compare handles these other tables... in other words, do further modifications need to be made, do the tables need to be rebuilt, or are they ignored altogether?
For the tables that are being transferred in the way you like, is it because you used schema mapping to force SQL Compare to map dbo to crm?

How do I add the identity property to an existing column in SQL Server

In SQL Server (in my case, 2005) how can I add the identity property to an existing table column using T-SQL?
Something like:
alter table tblFoo
alter column bar identity(1,1)
I don't beleive you can do that. Your best bet is to create a new identity column and copy the data over using an identity insert command (if you indeed want to keep the old values).
Here is a decent article describing the process in detail:
http://www.mssqltips.com/tip.asp?tip=1397
The solution posted by Vikash doesn't work; it produces an "Incorrect syntax" error in SQL Management Studio (2005, as the OP specified). The fact that the "Compact Edition" of SQL Server supports this kind of operation is just a shortcut, because the real process is more like what Robert & JohnFX said--creating a duplicate table, populating the data, renaming the original & new tables appropriately.
If you want to keep the values that already exist in the field that needs to be an identity, you could do something like this:
CREATE TABLE tname2 (etc.)
INSERT INTO tname2 FROM tname1
DROP TABLE tname1
CREATE TABLE tname1 (with IDENTITY specified)
SET IDENTITY_INSERT tname1 ON
INSERT INTO tname1 FROM tname2
SET IDENTITY_INSERT tname1 OFF
DROP tname2
Of course, dropping and re-creating a table (tname1) that is used by live code is NOT recommended! :)
Is the table populated? If not drop and recreate the table.
If it is populated what values already exist in the column? If they are values you don't want to keep.
Create a new table as you desire it, load the records from your old table into your new talbe and let the database populate the identity column as normal. Rename your original table and rename the new one to the correct name :).
Finally if the column you wish to make identity currently contains primary key values and is being referenced already by other tables you will need to totally re think if you're sure this is what you want to do :)
There is no direct way of doing this except:
A) through SQL i.e.:
-- make sure you have the correct CREATE TABLE script ready with IDENTITY
SELECT * INTO abcTable_copy FROM abcTable
DROP TABLE abcTable
CREATE TABLE abcTable -- this time with the IDENTITY column
SET IDENTITY_INSERT abcTable ON
INSERT INTO abcTable (..specify all columns!) FROM (..specify all columns!) abcTable_copy
SET INDENTITY_INSERT abcTable OFF
DROP TABLE abcTable_copy
-- I would suggest to verify the contents of both tables
-- before dropping the copy table
B) Through MSSMS which will do exactly the same in the background but will less fat-fingering.
In the MSSMS Object Explorer right click the table you need to modify
Select "design" Select the column you'd like to add IDENTITY to
Change the identity setting from NO -> YES (possibly seed)
Ctr+S the table
This will drop and recreate the table with all original data in it.
If you get a warning:
Go to MSSMS Tools -> Options -> Designers -> Table and database Designers
and uncheck the option "Prevent saving changes that require table re-creation"
Things to be careful about:
your DB has enough disk space before you do this
the DB is not in use (especially the table you are changing)
make sure to backup your DB before doing it
if the table has a lot of data (over 1G) try it somewhere else first
before using in real DB
Create a New Table
SELECT * INTO Table_New FROM Table_Current WHERE 1 = 0;
Drop Column from New Table
Alter table Table_New drop column id;
Add column with identity
Alter table Table_New add id int primary key identity;
Get All Data in New Table
SET IDENTITY_INSERT Table_New ON;
INSERT INTO Table_New (id, Name,CreatedDate,Modified)
SELECT id, Name,CreatedDate,Modified FROM Table_Current;
SET IDENTITY_INSERT Table_New OFF;
Drop old Table
drop table Table_Current;
Rename New Table as old One
EXEC sp_rename 'Table_New', 'Table_Current';
alter table tablename
alter column columnname
add Identity(100,1)

Resources