A simple stored procedure retrieves, say, 2 records - CCA & HRA under a column named ErnCode. Please note that in reality, as a programmer, I am not aware of the ErnCodes that will be actually retrieved from the 1st stored procedure while I am coding the stored procedure. I have framed a 2nd stored procedure that will retrieve only the HRA & CCA records from another table (since the 1st procedure retrieved only HRA & CCA). What I am doing in the 2nd procedure is first creating a temporary table & populating the temp table with the records got from the 1st procedure & then creating a CURSOR & populating the cursor with the records in the temp table. This is the 2nd procedure which I have framed:
CREATE PROCEDURE PSlip
@month varchar(5),
@year varchar(5)
AS
DECLARE
@sql varchar(7000),
@erncode varchar(20),
@ernname varchar(200)
CREATE TABLE #TempTable
(ErnCode varchar(20),ErnName varchar(200))
INSERT INTO #TempTable
EXEC Earnings @ccode --this is the 1st stored procedure
DECLARE ErnCodeCursor CURSOR
FOR SELECT ErnCode,ErnName FROM #TempTable
OPEN ErnCodeCursor
FETCH NEXT FROM ErnCodeCursor INTO @erncode,@ernname
WHILE(@@FETCH_STATUS=0)
BEGIN
SET @sql='SELECT DISTINCT(em.ECode),em.EName,attd.Basic,
(SELECT Amt FROM QL01ADET AS adet WHERE adet.ErnCode="' + @erncode + '" AND em.ECode=adet.ECode AND adet.Type IS NULL) AS "' + @ernname + '", FROM..................'
FETCH NEXT FROM ErnCodeCursor INTO @erncode,@ernname
END
SET @sql=REPLACE(@sql,", FROM"," FROM"
EXEC (@sql)
CLOSE ErnCodeCursor
DEALLOCATE ErnCodeCursor
Please note that the reason I am using the REPLACE function is because there will be an extra comma(,) just before the last FROM in the SQL query. To omit that comma, I am making use of the REPLACE function.
Now when I execute the procedure in the Query Analyzer using
EXEC PSlip 4,2001
only the CCA records get retrieved. The HRA records are not at all retrieved. Why is this happening? Shouldn't the FETCH NEXT FROM ErnCodeCursor then go to the HRA row so that the stored procedure can retrieve the HRA records? Where am I going wrong? What do I do to ensure that all the records from QL01ADET & QL_EMST tables get retrieved that correspond to the ErnCodes that have been populated in the ErnCodeCursor when the 2nd stored procedure is executed?
One again please note that in reality, as a programmer, I am not aware of the ErnCodes that will be actually retrieved from the 1st stored procedure while I am coding both the stored procedure.
Thanks,
Arpan
CREATE PROCEDURE PSlip
@month varchar(5),
@year varchar(5)
AS
DECLARE
@sql varchar(7000),
@erncode varchar(20),
@ernname varchar(200)
CREATE TABLE #TempTable
(ErnCode varchar(20),ErnName varchar(200))
INSERT INTO #TempTable
EXEC Earnings @ccode --this is the 1st stored procedure
DECLARE ErnCodeCursor CURSOR
FOR SELECT ErnCode,ErnName FROM #TempTable
OPEN ErnCodeCursor
FETCH NEXT FROM ErnCodeCursor INTO @erncode,@ernname
WHILE(@@FETCH_STATUS=0)
BEGIN
SET @sql='SELECT DISTINCT(em.ECode),em.EName,attd.Basic,
(SELECT Amt FROM QL01ADET AS adet WHERE adet.ErnCode="' + @erncode + '" AND em.ECode=adet.ECode AND adet.Type IS NULL) AS "' + @ernname + '", FROM..................'
FETCH NEXT FROM ErnCodeCursor INTO @erncode,@ernname
END
SET @sql=REPLACE(@sql,", FROM"," FROM"
EXEC (@sql)
CLOSE ErnCodeCursor
DEALLOCATE ErnCodeCursor
Please note that the reason I am using the REPLACE function is because there will be an extra comma(,) just before the last FROM in the SQL query. To omit that comma, I am making use of the REPLACE function.
Now when I execute the procedure in the Query Analyzer using
EXEC PSlip 4,2001
only the CCA records get retrieved. The HRA records are not at all retrieved. Why is this happening? Shouldn't the FETCH NEXT FROM ErnCodeCursor then go to the HRA row so that the stored procedure can retrieve the HRA records? Where am I going wrong? What do I do to ensure that all the records from QL01ADET & QL_EMST tables get retrieved that correspond to the ErnCodes that have been populated in the ErnCodeCursor when the 2nd stored procedure is executed?
One again please note that in reality, as a programmer, I am not aware of the ErnCodes that will be actually retrieved from the 1st stored procedure while I am coding both the stored procedure.
Thanks,
Arpan