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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.


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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
View SQL 2005 Job package 16 77
While in ##Table - Help 4 22
how to restore or keep sql2000  backups useful... 2 28
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

761 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