Execute a DML query DDL after. How to do this?

The error text:
Msg 2760, Level 16, State 1, Line 588
The specified schema name "copy" either does not exist or you do not have permission to use it.
Msg 208, Level 16, State 1, Line 607
Invalid object name 'copy.REPORTS'.
Msg 15151, Level 16, State 1, Line 664
Cannot find the object 'REPORTS', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Line 665
Cannot drop the schema 'copy', because it does not exist or you do not have permission.


Patches run from the command prompt using "sqlcmd"

In essence: Create a schema "copy", create a table that already exists in the database, but in the dbo schema. Further, from dbo.REPORTS the resulting data, we modify (there is a remove duplicates with the addition of some values). Put the modified data in the copy.REPORTS. Then delete dbo.REPORTS and copy.REPORTS change the schema to dbo.

Plus replacement schemes that do not need to rename anything, when you need to create the same table, no constraints, nothing. It is convenient.

When you run this query, when run from the command line (this part of the patch to the database), after creating the schema, then there is a bug that this schema, or no rights to its creation. How to fix? The script is run from under the Service administrator (sa).

IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'copy')
 EXECUTE( N 'CREATE SCHEMA copy')
GO

IF OBJECT_ID('copy.REPORTS', 'U') IS NOT NULL
 DROP TABLE copy.REPORTS;
GO

- The real table is a little different, this is from the lantern
CREATE TABLE copy.REPORTS (
 REPORT_DATE DATE NOT NULL
 ,REPORT_ID INT NOT NULL
 ,PK_REPORTS CONSTRAINT PRIMARY KEY CLUSTERED (REPORT_DATE, REPORT_ID)
) ON [PRIMARY]
GO

-- REQUEST MODIFIER, DATA FROM dbo.REPORTS and places them in the copy.REPORTS

DROP TABLE dbo.REPORTS; 
GO

ALTER SCHEMA dbo TRANSFER copy.REPORTS
GO

EXECUTE( copy'n DROP SCHEMA');
August 19th 19 at 22:40
1 answer
August 19th 19 at 22:42
Solution
It turned out that the problem was that all the code above is executed after changing the stored procedude (ALTER PROCEDURE), and after the code this change was not the GO statement. That's the whole problem and the code working.

Find more questions by tags SQL ServerProgrammingSQL