question

tiru5796 avatar image
tiru5796 asked

How to Write single proc for all operations?

I want to write Single stored proc for Insert,delete and update statements.Can you please help me?
updateinserthomeworkdelete
7 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image WilliamD commented ·
@Kev Riley - [MERGE command?][1] [1]: http://technet.microsoft.com/en-us/library/bb510625.aspx
3 Likes 3 ·
Mrs_Fatherjack avatar image Mrs_Fatherjack commented ·
Can you please give us some information on the code you wish to include?
2 Likes 2 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
Without wishing to sound flippant - why on earth would you want to do this?
2 Likes 2 ·
ThomasRushton avatar image ThomasRushton ♦♦ commented ·
+1 to Kev... ...and are you looking to write one procedure to do all of these operations against a single table, or are you wanting one procedure to run against all tables? (The first is relatively straightforward; the second, less so.)
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
@WilliamD - I hope you're right, but it didn't sound like it
0 Likes 0 ·
Show more comments
Grant Fritchey avatar image
Grant Fritchey answered
Have you looked at the MERGE command? That allows you to pass data through a single statement and then conditionally insert, update or delete as needed. Here's the MSDN entry: http://msdn.microsoft.com/en-us/library/bb510625.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

tiru5796 avatar image
tiru5796 answered
Thank you all for replies!!! I got the answer from my friend!!!
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
What was that answer? Edit your post and list it out so that others who are interested in the same thing that see your question will know what the answer was.
5 Likes 5 ·
tiru5796 avatar image
tiru5796 answered
CREATE PROCEDURE spForAllOperations @StudentID INT , @SName CHAR(20) , @SAddress CHAR(40) , @PresentClassID INT , @SubjectID INT , @SMarks INT , @Flag INT AS IF @Flag = 1 -- @Flag=1 Inserting the Compltely new record into the database tables BEGIN INSERT INTO Student VALUES ( @StudentID, @SName, @SAddress ) INSERT INTO Student_Marks VALUES ( @StudentID, @PresentClassID, @SubjectID, @SMarks ) END ELSE IF @Flag = 2 --- @Flag=2Inserting the already existing record of the student BEGIN INSERT INTO Student_Marks VALUES ( @StudentID, @PresentClassID, @SubjectID, @SMarks ) END ELSE IF @Flag = 3 --- @Flag=3 Updating the Marks and Address of the Student BEGIN UPDATE Student SET Address = @SAddress WHERE Stud_Id = @StudentID UPDATE Student_Marks SET Marks = @SMarks WHERE Subject_Id = @SubjectID AND Class_Id = @PresentClassID AND Stud_Id = @StudentID END In this way I will pass all the variables from my VB6 application with an extra reference to Flag . So If that condition satisfies then the action will perform otherwise don't. In this way We can write only one Stored proc for all the operations.
6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
But if you've already worked out that the student exists (to set the right flag), why not call a different sp?
4 Likes 4 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
But all queries within the procedure you outlined will be compiled the first time that procedure is called. That's how it absolutely works.
4 Likes 4 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
Of course, everything in a single procedure like this means that all queries are compiled at the same time with a single set of parameter values. You might see poor performance for some queries since they'll be compiled with less than optimal values.
1 Like 1 ·
tiru5796 avatar image tiru5796 commented ·
Yes, You can call other SPs also but it is the one way of writing less number of stored procs instead of different Sps for different operations.
0 Likes 0 ·
tiru5796 avatar image tiru5796 commented ·
@Grant: No all the queries are not complied at the same time.One query executed at a time based on the condition satisified in the IF CALUSE.ok
0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.