when make drop to table already exist on begin of procedure it not working issue until i do by hand ?

ahmed salah 3,216 Reputation points
2021-11-09T08:53:47.94+00:00

I work on SQL server 2012 i face issue
when make drop to table on begin of procedure it not working issue until I do by hand ?
so if i alter table Extractreports.dbo.PartGeneration by adding new column as onlineid on other place
then execute [Parts].[sp_get_parts] it give me error invalid column name onlineid
ok but i make drop on start of procedure why error display
so when i go on server then execute following statment as below
and run

IF OBJECT_ID('Extractreports.dbo.PartGeneration') IS NOT NULL
    DROP TABLE Extractreports.dbo.PartGeneration

    IF OBJECT_ID('Extractreports.dbo.getInsertedRows') IS NOT NULL
    drop table Extractreports.dbo.getInsertedRows

when execute again error not display
it working only when go every time and execute from server for drops tables

create Proc [Parts].[sp_get_parts]
    AS

        BEGIN

    IF OBJECT_ID('Extractreports.dbo.PartGeneration') IS NOT NULL
    DROP TABLE Extractreports.dbo.PartGeneration

    IF OBJECT_ID('Extractreports.dbo.getInsertedRows') IS NOT NULL
    drop table Extractreports.dbo.getInsertedRows
    --SOME LOGIC
select partid,companyname into Extractreports.dbo.PartGeneration from dbo.parts
select family,plname into Extractreports.dbo.getInsertedRows from dbo.parts
    END

so how to solve this issue

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,692 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 100.9K Reputation points MVP
    2021-11-09T22:49:14.827+00:00

    When you create a stored procedure, SQL Server will ignore if the procedure refers to non-existing tables, optimistically thinking that the table will appear at run-time. Which, in the case of temp table created in the procedure, can be very true.

    However, once that SQL Server finds a query where all tables exist, it will go on and check that all columns you refer to exist, so that you have not made any typos.

    If you are creating your table in the stored procedure, you need to drop it before you recreate it with a different definition of the table, or else you will get an error.

    0 comments No comments

  2. EchoLiu-MSFT 14,571 Reputation points
    2021-11-10T06:30:27.483+00:00

    You can first create a stored procedure to delete the table:

    CREATE PROCEDURE sp_dropifexists (@tableName VARCHAR(255))
    AS
    BEGIN
        DECLARE @SQL VARCHAR(MAX);
        SET @SQL = 'IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N''' + @tableName + ''') AND type = (N''U'')) DROP TABLE [' + @tableName + ']'
    
        EXEC (@SQL);
    
    END
    
    EXEC sp_DropIfExists 'Extractreports.dbo.PartGeneration'
    EXEC sp_DropIfExists 'Extractreports.dbo.getInsertedRows'
    

    Regards,
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments