Solved

More than one alter in a procedure

Posted on 2013-01-10
8
325 Views
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?
StorePolErrs.doc
0
Comment
Question by:BobRosas
  • 4
  • 2
  • 2
8 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
Comment Utility
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_'
0
 

Author Comment

by:BobRosas
Comment Utility
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?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
>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
0
 

Author Comment

by:BobRosas
Comment Utility
You are right!  Sorry!  Got it! Thanks!
I've max'd your points...what you guys have to deal with you deserve it!
Thanks!
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 26

Expert Comment

by:Chris Luttrell
Comment Utility
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.
0
 

Author Comment

by:BobRosas
Comment Utility
CGLuttrell
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!
0
 
LVL 26

Expert Comment

by:Chris Luttrell
Comment Utility
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
SQLServerPedia
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.
0
 

Author Comment

by:BobRosas
Comment Utility
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.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard 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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now