January 8, 2014 at 11:33 am
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...
January 8, 2014 at 11:48 am
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
January 8, 2014 at 12:03 pm
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)?
January 8, 2014 at 12:21 pm
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.
January 8, 2014 at 12:44 pm
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?
January 8, 2014 at 12:50 pm
You could set up a job in the SQL Server Agent.
January 8, 2014 at 1:09 pm
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.
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
January 8, 2014 at 1:28 pm
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
January 8, 2014 at 2:49 pm
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