Click here to Skip to main content
15,886,760 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
PRINT 'Before alter [ModelInfo]'
select ssm.definition
from sys.objects so, sys.sql_modules ssm
where so.name = 'ProcName'
and so.object_id = ssm.object_id

USE [productdb]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[ProcName]
(
@Name nvarchar(50),
@Birthday nvarchar(50)
)
RETURNS @return table
(

)
AS
BEGIN

declare
@Product nvarchar(50);
set @Product = '200'

END

PRINT 'After alter [ModelInfo]'
select ssm.definition
from sys.objects so, sys.sql_modules ssm
where so.name = 'ProcName'
and so.object_id = ssm.object_id

What I have tried:

hit the error for any function placed after the ALTER FUNCTION section. But if remove everything after ALTER FUNCTION, the script will run ok so having a separate SQL query it worked, can I executed PRINT before and PRINT after in 1 script instead of separate script Please advice. Thanks
Posted
Updated 12-May-23 23:41pm

1 solution

CREATE and ALTER statements must be the first and only statement in a query batch, you cannot add code above or below them within a batch.
The solution is simple: terminate the batch with GO to execute to that point, and a new batch begins immediately:
SQL
PRINT 'One'
GO
ALTER FUNCTION ReturnTheValue2()
RETURNS INT
AS
BEGIN
	RETURN 2
END
GO
PRINT 'Two'
GO
 
Share this answer
 
Comments
OriginalGriff 13-May-23 7:04am    
Do you want to try saying that again?
I have no idea what you are talking about:
"after added the GO like below can read but still cannot read inside before and after is nothing inside but executed the alter function looks ok can worked so the alter function no problem"

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Caiping Kong 13-May-23 8:08am    
sorry my question did not clear, btw thank you I will solve it by myself

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