SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored Procedure drop table


Stored Procedure drop table

Author
Message
brandy.britt
brandy.britt
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 12
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 :-D

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...
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28385 Visits: 39963
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!

brandy.britt
brandy.britt
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 12
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)?
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16673 Visits: 19108
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
brandy.britt
brandy.britt
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 12
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?
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16673 Visits: 19108
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
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8627 Visits: 7660
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
brandy.britt
brandy.britt
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 12
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
brandy.britt
brandy.britt
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 12
Thanks guys for all your help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search