Stored Procedure drop table

  • I am attempting to create a stored procedure that checks for a table and drops the table if it exist then creates the table and then gets data from a view and inserts it into a table on intervals of time, I will say each day.

    What I have may be way off but this is where I am at... don't laugh 😀

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    go

    ALTER PROCEDURE [dbo].[usp_PickHistoryReportTbl]

    AS

    BEGIN

    IF OBJECT_ID('Tbl_PickHist','U') IS NOT NULL

    DROP TABLE Tbl_PickHist

    GO

    CREATE TABLE dbo.Tbl_PickHist

    (LastPick datetime,

    ComponentItemNumber varchar(50),

    IssuedQuantity float)

    GO

    INSERT INTO dbo.Tbl_PickHist (LastPick,ComponentItemNumber,IssuedQuantity)

    select * from S_PickHistory2

    So currently when I try to alter the procedure it tells me the table already exist and it says my syntax is wrong in a couple places... I am sure this is elementary for some...

  • i don't userstand the logic of dropping it and adding it...why not delete, reseed any identities, and insert into again?

    your error is due to the fact that

    a stored proc cannot have the "GO" command in it;

    the "GO" command is actually for SSMS,a nd is not a valid TSQL comand.

    removing the GO removes the error:

    ALTER PROCEDURE [dbo].[usp_PickHistoryReportTbl]

    AS

    BEGIN

    DECLARE @cmd VARCHAR(MAX)

    IF OBJECT_ID('Tbl_PickHist','U') IS NOT NULL

    BEGIN

    DROP TABLE Tbl_PickHist

    CREATE TABLE dbo.Tbl_PickHist

    (LastPick datetime,

    ComponentItemNumber varchar(50),

    IssuedQuantity float)

    INSERT INTO dbo.Tbl_PickHist (LastPick,ComponentItemNumber,IssuedQuantity)

    SELECT * FROM S_PickHistory2

    END --IF

    END --PROC

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I thought that was a normal method... I don't know another answer..

    Is that better as far as best practice?

    This tells me incorrect syntax near MAX

    I have SQL 2000 ... should I use VARCHAR(8000)?

  • You actually don't need the following line

    DECLARE @cmd VARCHAR(MAX)

    As said, you would be better deleting the information from the table. You could use TRUNCATE TABLE for performance, but it has its limitations.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you!!!

    I will look at changing it....

    How can I cause it to perform every day? Is that just via a task in windows?

  • You could set up a job in the SQL Server Agent.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Instead of dropping the table, simply TRUNCATE it. That'll reset all your seeds and the like and you won't have to redefine the schema constantly, which fries dependencies and a few other concerns.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • ALTER PROCEDURE [dbo].[usp_PickHistoryReportTbl]

    AS

    BEGIN

    TRUNCATE TABLE Tbl_PickHist;

    INSERT INTO dbo.Tbl_PickHist (LastPick,ComponentItemNumber,IssuedQuantity)

    SELECT * FROM S_PickHistory2

    END

    This works

  • Thanks guys for all your help

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

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