Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Regarding stored procedure Expand / Collapse
Author
Message
Posted Tuesday, November 27, 2012 6:11 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 4, 2012 8:00 AM
Points: 13, Visits: 14
I am using two sql statements in stored procedure,
the two sql query is update and alter.
in some times i need to execute update query only,at that time how to execute stored procedure

Post #1389141
Posted Tuesday, November 27, 2012 6:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 12,905, Visits: 32,161
boobalanmca07 (11/27/2012)
I am using two sql statements in stored procedure,
the two sql query is update and alter.
in some times i need to execute update query only,at that time how to execute stored procedure



ALTER? to alter a table, it would only need to occur once, ever, in the database; doesn't really belong in the procedure.
otherwise you end up having to test every time whether to do it or not, which, since it will occur once, wastes a little bit of time checking to see if it needs to be done.


so my advice is to rethink where to put the alter statement...i don't think it belongs in a procedure.

regardless, you'd have to do something like this:
CREATE PROCEDURE MyProc(@paramters int)
AS
BEGIN
IF NOT EXISTS (SELECT 1
from sys.objects objz
left outer join sys.columns colz
on objz.object_id = colz.objectid
where objz.name = 'MyTable'
and colz. name = 'MyColumn')
BEGIN --IF
ALTER TABLE MyTable ADD MyColumn sqlvariant
END --IF

UPDATE MyTable
SET SomeColumn = SomeValue
WHere AnotherColumn = @paramters
END --PROC




Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1389147
Posted Tuesday, November 27, 2012 6:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:55 AM
Points: 2,873, Visits: 5,185
Could you please post your stored procedure code?
Without seeing what you can see it's hard to provide relevant help. Please read an article from the link at the bottom of my signature about this forum etiquette.

From your very vague description, I can only guess that you have to choices:

1. Create a separate stored proc which only performs UPDATE
2. Amend the existing one to have input parameter as a flag which will determine its behaviour.


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1389148
Posted Tuesday, November 27, 2012 6:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
I'd need to see the proc to be able to help out here.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1389159
Posted Tuesday, November 27, 2012 7:46 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:55 AM
Points: 2,873, Visits: 5,185
Instead of including the code here, OP just duplicated the post:

http://www.sqlservercentral.com/Forums/Topic1389161-149-1.aspx


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1389204
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse