Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
A RETURN statement with a return value cannot be used in this context.

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.


i am getting above error when i run the below code in sql



SQL
alter  proc Usp_Archied_new
@fromdate date,
@todate Date,
@name varchar(200)
as

BEGIN TRAN


if not exists(select * from sys.objects where type_desc='USER_TABLE' and name='Process_Trn_backup' and name ='Core_Process_Dtls_backup'  and name ='NonCore_Process_Dtls_Backup')
begin
    
alter table Process_Trn_backup add   createdby varchar(150),Createdon date
    ---- SOME OPERATIONS ---
    
    IF (@@ERROR <> 0) GOTO ERR_HANDLER
    
    
    ---- SOME OPERATIONS ---
    
    IF (@@ERROR <> 0) GOTO ERR_HANDLER
    
    
    ---- SOME OPERATIONS ---
    
    IF (@@ERROR <> 0) GOTO ERR_HANDLER
    
    
    
    ---- SOME OPERATIONS ---
    
    
    IF (@@ERROR <> 0) GOTO ERR_HANDLER
    
    
    ---- SOME OPERATIONS ---
    
    IF (@@ERROR <> 0) GOTO ERR_HANDLER

end

else

begin
alter table NonCore_Process_Dtls_Backup add   createdby varchar(150),Createdon date
      ---- SOME OPERATIONS ---
    
    IF (@@ERROR <> 0) GOTO ERR_HANDLER
    
      ---- SOME OPERATIONS ---

end



COMMIT TRAN

    RETURN 0

ERR_HANDLER:
    ROLLBACK TRAN
    RETURN 1
Posted
Updated 28-Mar-12 5:25am
v5
Comments
OriginalGriff 27-Mar-12 3:08am    
If you want anyone to read that, then I suggest that you at least indent it to make our lives a little easier.
At the moment it is a large code dump and nothing else.

I can see following alter statements in the sproc.

SQL
alter table Process_Trn_backup add   createdby varchar(150),Createdon date


SQL
alter table NonCore_Process_Dtls_Backup add   createdby varchar(150),Createdon date


You can not execute CREATE/ALTER statements directly in a stored procedure.
 
Share this answer
 
Comments
[no name] 28-Mar-12 11:22am    
yes your point is valid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900