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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2 service pack updates 5 64
Database Owner 3 45
Estimating my database size 7 51
What does "Between" mean? 6 47
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

751 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