Stored Procedure code understanding

  • Hi,

     

    I am working on a already written stored procedure and need to understand what exactly it means:

    Here is the script:

    set @query =' if object_id (lib.dbo.ABC )is not null drop table lib.dbo.ABC

    select column1, column,....column n into lib.dbo.ABC from '+ABC+.'

  • As per my understanding if table ABC already exist in a defined library it will delete it however my question is why it is giving me an error if table does not exist at all.

  • ng_rai wrote:

    Hi,   I am working on a already written stored procedure and need to understand what exactly it means: Here is the script: set @query =' if object_id (lib.dbo.ABC )is not null drop table lib.dbo.ABC select column1, column,....column n into lib.dbo.ABC from '+ABC+.'

    Do you know what an "object_id" is?

    To cut to the chase, the object_id in this query is being checked for NULL.  If the object (table, in this case) does exist, then it is dropped.  Either way, the SELECT/INTO is reading from a table into a non-existent table, which first creates the table and then populates it with the result of the SELECT.

    The key here is that the query code isn't actually being executed here.  It's being stored in the @query variable for execution later.

    The trouble here is that they're dropping table ABC if it exists and then using it as the source to recreate it.

    Are you sure this query is actually the way it is or did you make a mistake when you tried to simplify it for this post???

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • There are so many issues with this code, it's hard to know where to start.  Since you failed to provide the error message, it's even more difficult.

    1. You have an unclosed single quote.
    2. You have an unbound name ABC in your static portion.

      1. It can't be a table name, because you have no FROM clause.
      2. It can't be a column name, again because there is no FROM clause.
      3. It can't be a user-defined function, because you haven't supplied a schema.
      4. It's not a variable, because there is no @.

    3. Assuming that ABC refers to a column or table, it's invalid in a SET clause.

      1. You would need to use a subquery in order reference a column or table in a SET clause.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • It was a really long code and i couldn't copy the code over here.

    But here is the actual script :

     

    ALTER PROCEDURE [dbo].[SP_SBC_CY]

     

    AS

    BEGIN

    SET NOCOUNT ON;

    declare @CYMinus1Table varchar(50), @CYMinus2Table varchar(50), @CYMinus3Table varchar(50)

    set @CYMinus1Table = 'Accesstest.dbo.[ACAR Historical Data_' + (select cast(year(getdate()) - 2 as varchar(4))) + ']'

    set @CYMinus2Table = 'Accesstest.dbo.[ACAR Historical Data_' + (select cast(year(getdate()) - 3 as varchar(4))) + ']'

    set @CYMinus3Table = 'Accesstest.dbo.[ACAR Historical Data_' + (select cast(year(getdate()) - 4 as varchar(4))) + ']'

     

    declare @query varchar(max)

    set @query = '

    if object_id(''AccessTest.dbo.CYMinus1Table'') is not null drop table AccessTest.dbo.CYMinus1Table

    select

    [Company Number]

    ,[Company Name]

    ,[Total Equity] as TotalEquity_CYMinus1

    ,[Net Income] as NetIncome_CYMinus1

     

    into AccessTest.dbo.CYMinus1Table

    from ' + @CYMinus1Table + '

     

    if object_id(''AccessTest.dbo.CYMinus2Table'') is not null drop table AccessTest.dbo.CYMinus2Table

    select

    [Company Number]

    ,[Company Name]

    ,[Total Equity] as TotalEquity_CYMinus2

    ,[Net Income] as NetIncome_CYMinus2

    into AccessTest.dbo.CYMinus2Table

    from ' + @CYMinus2Table + '

     

    if object_id(''AccessTest.dbo.CYMinus3Table'') is not null drop table AccessTest.dbo.CYMinus3Table

    select

    [Company Number]

    ,[Company Name]

    ,[Total Equity] as TotalEquity_CYMinus3

    ,[Net Income] as NetIncome_CYMinus3

    into AccessTest.dbo.CYMinus3Table

    from ' + @CYMinus3Table + ''

     

    exec (@query)

  • Also, this is just part of the code and when I am running the complete Stored procedure I am getting this particular error:

     

    Msg 207, Level 16, State 1, Procedure SP_FlagTests_CY, Line 266 Invalid column name 'BaseSolvencyBuffer_CYMinus1'.

     

    Help on this too please.

  • ng_rai wrote:

    Also, this is just part of the code and when I am running the complete Stored procedure I am getting this particular error:   Msg 207, Level 16, State 1, Procedure SP_FlagTests_CY, Line 266 Invalid column name 'BaseSolvencyBuffer_CYMinus1'.   Help on this too please.

    That column name is not included in the code that you've provided, and there is no way to dynamically produce that column name from the provided code either.  The error is occurring elsewhere in your code.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • This was removed by the editor as SPAM

  • Hi I posted this answer before but it appears to be marked as SPAM.  Not sure why.  This site has an annoying text editor.

     

    What this code is doing.

    1. Declares 3 varchar(50) variables to store 3 tables names which are based off of the current year date.  If the sql is run today the 3 variables evaluate to:

    Accesstest.dbo.[ACAR Historical Data_2017]

    Accesstest.dbo.[ACAR Historical Data_2016]

    Accesstest.dbo.[ACAR Historical Data_2015]

    2. Declares a varchar(max) variable called @query to store dynamic sql

    3. Assigns 3 blocks of code to the @query variable.  One for each year table.

    The 3 blocks inside1 each do similar: Drop the existing 'AccessTest.dbo.CYMinus1Table' table (or ...2, or ...3 table) if it exists and then uses 'select into' to recreate the 'AccessTest.dbo.CYMinus1Table' using the table named variable (defined from the date above date)

    Run today this code will:

    1) Drop table if exists 'AccessTest.dbo.CYMinus1Table'

    2) Insert data from table Accesstest.dbo.[ACAR Historical Data_2017] into AccessTest.dbo.CYMinus1Table

    3) Drop table if exists 'AccessTest.dbo.CYMinus2Table'

    4) Insert data from table Accesstest.dbo.[ACAR Historical Data_2016] into AccessTest.dbo.CYMinus2Table

    5) Drop table if exists 'AccessTest.dbo.CYMinus3Table'

    6) Insert data from table Accesstest.dbo.[ACAR Historical Data_2015] into AccessTest.dbo.CYMinus3Table

    It appears there is a year missing in this scheme!

     

    • This reply was modified 5 years, 4 months ago by  Steve Collins.

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply