TABLE DROP statement - sql

I am trying to drop a table that I created in SQL Server. I typed the command as follows:
DROP TABLE [Exercise2].
I received the following error message:
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Exercise2', because it does not exist or you do not have permission.
I have dropped multiple tables trying to clean out my database, but this one will not drop and I'm not sure why.

Related

using new columns in the temp table added by alter table not work

I have a problem that the new added column can't be used in the further comments.
I have a temp table built by "select into" then I need to add an identity column by "alter table". But when I want to use the new column in a "join", I got an error "Invalid column". please note that, these commands could work separately.
I think the reason is, the new column is not found by the compiler and it give an error before running it.
Is there a solution for that ?
I have got this problem in sql server 2000 and it seems in a newer version, the problem is not there.
create table #tmp_tb
(name varchar(4), val int)
insert into #tmp_tb values('ab',1);
insert into #tmp_tb values('abc',2);
select * from #tmp_tb
alter table #tmp_tb add id int NOT NULL IDENTITY(1,1);
select * from #tmp_tb
select id,name,val from #tmp_tb
An error occurred :
Msg 207, Level 16, State 3, Line 9
Invalid column name 'id'.
Replace the last line with
EXECUTE sp_executesql N'select id,name,val from #tmp_tb';
Indeed, the parser doesn't know about the new column yet. By passing it through sp_executesql you avoid this.

View Statement on SQL Server returns "Invalid Object Name"

When executing the following script on my SQL Server:
CREATE VIEW joiny AS
SELECT EventTime
FROM [dbo].[Table_1]
I get the following error:
Invalid object name 'Table_1'.
I cannot figure out why this is an error. Could anyone point me in the right direction? I tried with and without the [] as well as the "dbo".
This error can mean that the table [dbo].[Table_1] simply does not exist in the database you are using.
CREATE VIEW tabtest
AS
SELECT *
FROM dbo.TabTest
Results in:
Msg 208, Level 16, State 1, Procedure tabtest, Line 4 Invalid object
name 'dbo.TabTest'.
Because I do not have a table named dbo.tabtest in my database.\
If you are trying to create a view in a different database than the one where your table exists, then you need to use 3-part naming when you specify the table, like this:
CREATE VIEW joiny AS
SELECT EventTime
FROM [MyDatabaseName].[dbo].[Table_1]

Invalid object name when updating after renaming table

Have a very strange problem when trying to rename table and use it.
I have a table called oldTable and rename it to the newTable.
I can successfully use select on this table using:
SELECT * FROM database.dbo.newTable;
But when I try to use update like this:
UPDATE database.dbo.newTable SET foo = bar where id = 1;
I receive following error:
Msg 208, Level 16, State 1, Procedure archive, Line 4 [Batch Start Line 0]
Invalid object name 'oldTable'.
Looks like name oldTable was stored somewhere and is used here by some kind of reference. It happens on both ssms and raw php+sql when trying to update.
Anyone have any idea?
Renaming a table will not update any Triggers that have been defined for the table (or any references to it anywhere else) so you need to manually update any triggers or other dependencies to reflect the new name.

How do I get rid of an orphaned default constraint in T-SQL?

I am trying to create a script in order to create a table in SQL Express 2008 R2, like so:
IF (EXISTS (SELECT * FROM sys.tables WHERE name='Table' AND [type]='U')) BEGIN
DROP TABLE Table
END
GO
CREATE TABLE Table (
col DATETIMEOFFSET CONSTRAINT DF_Table_col DEFAULT GETUTCDATE()
)
GO
I am trying to follow the practice of creating a script that can be re-executed for this release cycle of my project.
But the problem is that I forgot to drop the default constraint DF_Table_col before dropping the table "Table", which I would write as:
IF (EXISTS (SELECT * FROM sys.default_constraints WHERE name='DF_Table_col')) BEGIN
ALTER TABLE Table DROP CONSTRAINT DF_Table_col
END
GO
Now if I try to re-run the script, I get the following error:
Msg 2714, Level 16, State 5, Line 1
There is already an object named 'DF_Table_col' in the database.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
How do I get rid of the orphaned constraint DF_Table_col?
I tried to bind this orphan back to a new table by the same name, like so:
exec sp_bindefault 'DF_Table_col', 'Table.col'
but it gives me the following error:
Msg 15050, Level 11, State 1, Procedure sp_bindefault, Line 108
Cannot bind default 'DF_Table_col'. The default must be created using the CREATE DEFAULT statement.
as far as DF_Table_col will be deleted if 'Table' is deleted and it belong to 'Table' there might be 'DF_Table_col'. on an other Table.
If you don't care about the name you can use
CREATE TABLE Table (
col DATETIMEOFFSET DEFAULT GETUTCDATE()
)
GO

How - create and use database directly after creation in SQL Server?

I created a sql script to check if a database already exist, if it already exists it deletes and re-creates.
After I would like to connect it directly after its creation for creating tables ..
Here is my code but it does not work.
He announces an error message
Msg 911, Level 16, State 1, Line 10
Database 'Arms2' does not exist. Make sure that the name is entered correctly.
My script
IF EXISTS (select * from sys.databases where name = 'Arms2')
BEGIN
DROP DATABASE Arms2
PRINT 'DROP DATABASE Arms2'
END
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
USE Arms2
CREATE TABLE .....
Put a GO statement after the CREATE...
...
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
GO
USE Arms2