Question : SQL Function Error - Cannot access temporary tables from within a function

I need someay to create a function with the components I have in my base SQL.

Getting the following error:

Msg 2772, Level 16, State 1, Procedure cusfn_FilingHistory, Line 31
Cannot access temporary tables from within a function.
Msg 154, Level 15, State 3, Procedure cusfn_FilingHistory, Line 31
an INTO clause is not allowed in a cursor declaration.
Msg 2772, Level 16, State 1, Procedure cusfn_FilingHistory, Line 42
Cannot access temporary tables from within a function.
Msg 2772, Level 16, State 1, Procedure cusfn_FilingHistory, Line 73
Cannot access temporary tables from within a function.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
USE [Demo04]
GO
/****** Object:  UserDefinedFunction [dbo].[cusfn_Responses]    Script Date: 07/13/2010 15:45:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE   FUNCTION [dbo].[cusfn_FilingHistory] ( @PatientVisitId INT )
RETURNS VARCHAR(8000)
AS BEGIN 
 
      DECLARE
         @TicketNumber VARCHAR(20),
         @FilingType VARCHAR(10),
         @ClearingHouseName VARCHAR(255),
         @FiledTo VARCHAR(255),
         @FiledBy VARCHAR(30),
         @ClaimDate DATETIME,
         @ProceduresFiled VARCHAR(255),
         @Charges MONEY,
         @Result VARCHAR(8000) ; 
  
      SET @Result = ''

      DECLARE MyCursor CURSOR LOCAL FAST_FORWARD
         FOR SELECT
               pv.TicketNumber,
               'Electronic' AS FilingType,
               ch.ClearinghouseName,
               ic.ListName AS FiledTo,
               ecf.FiledBy,
               ecf.FileTransmitted AS ClaimDate,
               ec.Procedures AS ProceduresFiled,
               ec.Charges AS Charges
             INTO
               #temp
             FROM
               EDIClaimFile ecf 
             INNER JOIN EDIClaim ec ON ecf.EDIClaimFileId = ec.EDIClaimFileId 
             INNER JOIN InsuranceCarriers ic ON ec.InsuranceCarriersId = ic.InsuranceCarriersId 
             INNER JOIN patientvisit pv ON ec.patientvisitID = pv.patientvisitID 
             INNER JOIN clearinghouse ch ON ecf.clearinghouseID = ch.clearinghouseID
             WHERE
               pv.PatientVisitId = @PatientVisitId

      INSERT INTO
         #temp
         (
           TicketNumber,
           FilingType,
           ClearinghouseName,
           FiledTo,
           FiledBy,
           ClaimDate,
           ProceduresFiled,
           Charges
	
         )
         SELECT
            pv.TicketNumber,
            'Paper' AS FilingType,
            '' AS ClearinghouseName,
            ISNULL(pvpc.Name , 'No Carrier') AS FiledTo,
            pvpc.createdby AS FiledBy,
            pvpc.created AS ClaimDate,
            pvpc.Procedures AS ProceduresFiled,
            pvpc.Charges AS Charges
         FROM
            PatientvisitPaperClaim pvpc 
         INNER JOIN patientvisit pv ON pvpc.patientvisitID = pv.patientvisitID
         WHERE
            pv.PatientVisitId = @PatientVisitId
   
      SELECT
         *
      FROM
         #temp
         
      OPEN MyCursor  
  
      FETCH NEXT FROM MyCursor INTO @TicketNumber , @FilingType , @ClearingHouseName , @FiledTo , @FiledBy , @ClaimDate , @ProceduresFiled , @Charges ; 
      WHILE @@FETCH_STATUS = 0
         BEGIN
            SET @Result = @Result 
            + 'Ticket: ' + LEFT(ISNULL(@TicketNumber,'') + SPACE(10),10) 
            + 'Type: ' + @FilingType + SPACE(5)
            + 'CL House: ' + LEFT(ISNULL(@ClearingHouseName,'') + SPACE(15),15)
            + 'FiledTo: ' + LEFT(ISNULL(@FiledTo,'') + SPACE(40),40)
            + 'FiledBy: '  + LEFT(ISNULL(@FiledBy,'') + SPACE(20),20)
            + 'ClaimDate: ' + LEFT(ISNULL(CONVERT(VARCHAR(15) , @ClaimDate , 101) , '') + SPACE(15) , 15)
            + 'Procedures: ' + LEFT(ISNULL(@ProceduresFiled,'') + SPACE(60),60)
            + CHAR(13) + CHAR(10) ;
            FETCH NEXT FROM MyCursor INTO @TicketNumber , @FilingType , @ClearingHouseName , @FiledTo , @FiledBy , @ClaimDate , @ProceduresFiled , @Charges  ; 
         END ;

      CLOSE MyCursor ;
      DEALLOCATE MyCursor ;

      RETURN RTRIM(@Result) ;
   END

Answer : SQL Function Error - Cannot access temporary tables from within a function

phpmyadmin, will stop on errors when you are importing a file, there are no options to set it to continue processing expect for "Ignore Duplicates".

You can write your own php script to loop read the CSV file and insert the records into the database.
That way you can continue to process the records even if there are errors.
Random Solutions  
 
programming4us programming4us