Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


More than one alter in a procedure

Posted on 2013-01-10
Medium Priority
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 2000 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.


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 27

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 27

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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