More than one alter in a procedure

Posted on 2013-01-10
Last Modified: 2013-01-10
With the help of EE (Thank you!) I have the attached code that works great when I run it manually as a SQL query.  But now I'd like to automatically run it on a daily basis.  I only have one "alter procedure" so I thought I could put it all in one procedure but the procedure only shows up to the "alter view".  What would be the best way to do this?
Question by:BobRosas
  • 4
  • 2
  • 2
LVL 75

Accepted Solution

Aneesh Retnakaran earned 500 total points
ID: 38764336
ALTER TABLE dbo.tblLoadLogErrors ADD PollId INT IDENTITY (1,1)
EXEC('ALTER VIEW dbo.vwLoadLogErrors AS SELECT PollDate, PollTime, Store FROM dbo.tblLoadLogErrors')

also get rid of the 'GO' inside the procedure;

You should also consider renaming the procedure something which donot stat with 'sp_'

Author Comment

ID: 38764443
Thank you so much for your help.  The procedure  (in part brloe) now includes the alter code and looks like this
print @sql
exec (@sql)

EXEC('ALTER TABLE dbo.tblLoadLogErrors ADD PollId INT IDENTITY (1,1)')

EXEC('ALTER VIEW dbo.vwLoadLogErrors AS SELECT PollDate, PollTime, Store FROM dbo.tblLoadLogErrors')

But it still cuts off the rest of the query.  Do I make the remaining code another sp then schedule one after the other?
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38764576
>But it still cuts off the rest of the query
Please remove the "GO' within the procedure; Remember 'GO' is a batch terminator and Procedure is a batch
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


Author Comment

ID: 38764607
You are right!  Sorry!  Got it! Thanks!
I've max'd your points...what you guys have to deal with you deserve it!
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38764617
You do not have to alter the table and view everytime you run the procedure, once they have been altered or created they will be there unless you are dropping and recreating the table also some where.
I also wholeheartedly agree with not naming your procedures with the "sp_" prefix, those are special to Microsoft and can cause performance issues.
Two questions, first, what part of this are you wanting to be run everyday? The load into your tblLoadLogErrors table or the query result? And secondly, are you familiar with Jobs in SQL Server Agent and have permissions to set them up?  That is the way I would approach it.

Author Comment

ID: 38764730
Thank you again for your input.  I was hoping if I could fit all the code in one sp I could use the sp as the dataset for a report in report services and just have the report scheduled to be delivered via e-mail every day.  Each day the report was delivered it would run the sp?  Maybe that won't work?

But yes everyday a new log is generated so I'll need to recreate the table daily.  Then e-mail it just one time every day.

Also I know very little about SQL Server Agent.  I believe you can create a job that runs a stored procedure.  I'm guessing my "report services" way won't work and I should investigate SQL Server Agent more.  Thanks for pointing me in the right direction.

I did not know that about the naming convention.  I thought it was good practice to use 'sp' so you know where the data is coming from when you see the name in code.  Now that I know that is not true I will stop doing that.  Again...Thank you both for all your help!
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38764789
It is good to have naming conventions and "spXXX" if fine, just don't use the underscore, as pointed out in these links.
MSDN Link on SP_
SQL Server Central
In the Naming Stored procedures section of Books Online:

We recommend that you do not create any stored procedures using sp_ as a prefix. SQL Server uses the sp_ prefix to designate system stored procedures. The name you choose may conflict with some future system procedure. If your application uses nonschema qualified name references and your own procedure name conflicts with a system procedure name, your application will break because the name binds to the system procedure, not your own.

I can try to help you with setting up a SQL Job later after I get off work.

Author Comment

ID: 38764839
Got it...just no underscore...thank you!

You know I'd appreciate any help I can get with the SQL Job but I just noticed that my SQL Server Agent is diabled.  So I'll try to figure that out then post a link here to my next question?  I'd rather you earn points for your hard work.  
What ever happened to related question posts?  Maybe I'm just not seeing it.

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server running out of memory - Something is consuming all the available memory 17 61
export sql results to csv 6 36
SQL Help 27 45
Query Syntax 17 36
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question