Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Begin and END in sql 2005.

Author  Topic 

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-05-11 : 16:17:25
This is a really dumb question, that I just realized I do not know the answer to.

Inside of a stored procedure, The use of Begin and end is required for numourus things, but is it required in this situation

Lets say I have two procudres, one of which must run before the second

exec SP_MustRunFirstAndComplete
Exec SP_RunsSecond

--should id be this?
Begin
exec SP_MustRunFirstAndComplete
End
Begin
Exec SP_RunsSecond
End



Is there any need for a begin end in this scenerio, or does sql know it must complete the first procedure before it starts the second on it's own?

also the same question with two queries

--Must run and Complete before the update
Insert into TBl1(Column123)
select colum123
from tbl2

--First query must be done in full before this runs.
Update Tbl1(Column123)
set Column999 = 12345


Do I need Begin and ends in any of these scenerios? and if I do, would I write it like so


BEGIN
--Must run and Complete before the update

Insert into TBl1(Column123)
select colum123
from tbl2

--First query must be done in full before this runs.
Update Tbl1(Column123)
set Column999 = 12345
END

--or
BEGIN
--Must run and Complete before the update
Insert into TBl1(Column123)
select colum123
from tbl2
END
BEGIN
--First query must be done in full before this runs.
Update Tbl1(Column123)
set Column999 = 12345
END


Thanks a lot for the clarification

ModemRat
Starting Member

4 Posts

Posted - 2007-05-11 : 16:25:17
The BEGIN and END statements are used to group multiple statements into one block. These are used frequently in IF statements such as:

IF(1=1)
BEGIN
print'One Does really equal One'
END
ELSE
BEGIN
print 'This will never happen and you know it'
END

Stored procedures will not run asyncronously. Once the first one completes it will then proceed to execute the second.


Also, your insert and update statements will not execute at the same time. Once the insert has completed the update will execute.
Go to Top of Page
   

- Advertisement -