Hi,
I have some stored procedure and there is a cursor inside it. I added some new columns to the table and those columns I included in the cursor declaration and fetch statement. In the cursor declaration I forgot to add comma (,) in between the new columns. So SQL Server it considers as a alias name for that column so syntactically it is correct. But logically in the cursor declaration having less number of columns than the columns in the fetch statement. So it should throw an error. But the procedure is getting compiled without raising any error. But if I execute the procedure that time it is throwing the error.
For example, below I have given the sample procedure. In this procedure, in the cursor declaration I removed the comma (,) between DOB and DOJ. If I compile this procedure it is getting compiled. But when execute that time only it is throwing the error. So I am interested in if any option is available to know the error in the compilation time itself.
ALTER PROCEDURE Test
AS
BEGIN
BEGIN TRY
DECLARE @empId INT,
@fname VARCHAR(50),
@dob DATE,
@doj DATE
DECLARE c_Emp CURSOR FAST_FORWARD FOR
SELECT EmpId, FName, DOB DOJ FROM Employee
OPEN c_Emp
FETCH NEXT FROM c_Emp INTO @empId,@fname,@dob,@doj
WHILE (@@FETCH_STATUS=0)
BEGIN
PRINT @fname
FETCH NEXT FROM c_Emp INTO @empId,@fname,@dob,@doj
END
CLOSE c_Emp
DEALLOCATE c_Emp
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
CLOSE c_Emp
DEALLOCATE c_Emp
END CATCH
END
I have some stored procedure and there is a cursor inside it. I added some new columns to the table and those columns I included in the cursor declaration and fetch statement. In the cursor declaration I forgot to add comma (,) in between the new columns. So SQL Server it considers as a alias name for that column so syntactically it is correct. But logically in the cursor declaration having less number of columns than the columns in the fetch statement. So it should throw an error. But the procedure is getting compiled without raising any error. But if I execute the procedure that time it is throwing the error.
For example, below I have given the sample procedure. In this procedure, in the cursor declaration I removed the comma (,) between DOB and DOJ. If I compile this procedure it is getting compiled. But when execute that time only it is throwing the error. So I am interested in if any option is available to know the error in the compilation time itself.
ALTER PROCEDURE Test
AS
BEGIN
BEGIN TRY
DECLARE @empId INT,
@fname VARCHAR(50),
@dob DATE,
@doj DATE
DECLARE c_Emp CURSOR FAST_FORWARD FOR
SELECT EmpId, FName, DOB DOJ FROM Employee
OPEN c_Emp
FETCH NEXT FROM c_Emp INTO @empId,@fname,@dob,@doj
WHILE (@@FETCH_STATUS=0)
BEGIN
PRINT @fname
FETCH NEXT FROM c_Emp INTO @empId,@fname,@dob,@doj
END
CLOSE c_Emp
DEALLOCATE c_Emp
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
CLOSE c_Emp
DEALLOCATE c_Emp
END CATCH
END