Skumar
Skumar

Reputation: 1

SQL Server stored procedure error . invalid identifier

I have a stored procedure, but when I execute it from my front-end I get this error:

The name 'CREATE TABLE tmp_148_58 (affili_item_id varchar(250),academic_id varchar(250),college_id varchar(250),item_value_quantity_college_entry varchar(250),item_value_notes_college_entry varchar(250),college_enter_on varchar(250),college_enter_by varchar(250),affili_category_colleges_autoid varchar(20))' is not a valid identifier.

My procedure code:

ALTER PROCEDURE [dbo].[SpPortal_AppForAffi_Upd_Both_Lbl_And_Vals1]
   (@columnList TEXT
    ,@insScript nvarchar(1000)
    ,@collegeId INT
    ,@LoginId BIGINT)
AS
BEGIN
   DECLARE 
       @tmpTableName VARCHAR(200),
       @effectCount INT = 0, 
       @effectCountTotal INT = 0,
       @ExeQuery nvarchar(1000),
       @InsertQuery nvarchar(1000)

    SET @tmpTableName = CONCAT('#tmp_',@collegeId,'_',@LoginId);

    SET @ExeQuery = CONCAT('DROP TABLE IF EXISTS ', @tmpTableName);             

    EXECUTE @ExeQuery ;

    -- create temp table..  --
    SET @ExeQuery = CONCAT ('CREATE TABLE ' , @tmpTableName , ' (',@columnList,')' ) ;       -- here column list should be come from froent end...
    EXECUTE @ExeQuery;

    -- # create temp table..   --
    --  load data into temp table --
    SET @InsertQuery =  CONCAT(' ' , @insScript);

    EXECUTE @InsertQuery; 

    -- # load data into temp table.. --
    -- updating  affili_items_colleges master table--
    SET @effectCount=0;
    -- SET param_sp_success=0;
    Begin TRANSACTION
         Begin Try
        -- SET param_sp_success = 0;
        SET @effectCount = 0;
        SET @effectCountTotal = 0;

        SET @ExeQuery = CONCAT(' UPDATE ', @tmpTableName,' AS tmp ,affili_item_label afil,affili_items afi
        SET afil.item_lable_name = tmp.item_value_quantity_college_entry 
    ,afil.enter_on=tmp.college_enter_on 
    ,afil.enter_by= tmp.college_enter_by
    WHERE  tmp.affili_item_id=afil.affili_item_id AND tmp.affili_item_label_id = afil.affili_item_label_id 
    AND afi.is_label = 1 AND  tmp.academic_id=afil.academic_id  AND tmp.college_id=afil.college_id  
    AND tmp.affili_item_id = afi.affili_item_id  AND afi.active_status = 1 ');    

    EXECUTE @ExeQuery;

    SET @ExeQuery = CONCAT(' UPDATE ', @tmpTableName,' AS tmp ,affili_items_colleges afic,affili_items afi
    SET afic.item_value_quantity_college_entry = tmp.item_value_quantity_college_entry 
    ,afic.item_value_notes_college_entry=tmp.item_value_notes_college_entry 
     ,afic.college_enter_on=tmp.college_enter_on 
     ,afic.college_enter_by= tmp.college_enter_by
    WHERE tmp.affili_item_id=afic.affili_item_id AND tmp.affili_item_label_id = afic.affili_item_label_id 
     AND tmp.academic_id=afic.academic_id  AND tmp.college_id=afic.college_id  
     AND tmp.affili_item_id = afi.affili_item_id AND afi.is_label <> 1 AND afi.active_status = 1 ');      

    EXECUTE @ExeQuery;      

    declare @te int=0

    SET @ExeQuery = CONCAT ('SELECT  COUNT(tem.affili_item_id)  INTO @te  
    FROM ',@tmpTableName,' tem INNER JOIN affili_items afi ON tem.affili_item_id = afi.affili_item_id AND afi.is_label <> 1 
    WHERE afi.active_status = 1 ') ; 

    EXECUTE @ExeQuery;

     SET  @effectCount=0;
     SET @effectCount =  @te ;

         IF(@effectCount>0) 
         BEGIN

         SET @effectCountTotal= @effectCount+1;

         END

    -- SET param_sp_success = effectCountTotal;
 IF(@@TRANCOUNT>0)
        BEGIN
            COMMIT TRANSACTION
        END
    ELSE
        BEGIN
            ROLLBACK TRANSACTION
        END
    END TRY

    BEGIN CATCH
        ROLLBACK TRANSACTION
    END CATCH

END

Can anyone help me out in solving it? I converted above query from mysql to SQL Server.

Upvotes: 0

Views: 296

Answers (1)

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

First of all - I really wondering why you are using all of this dynamically created statements. As I can see from your script - the only reason for it is unique name of temporary table you're creating.

But you don't really need your temporary table to have unique name, as this table is visible only in the scope of stored procedure where it was created (and also in the scope of 'child' procedures called from that one).

Also, as per your error it looks like your script tries to create real, not temporary table - see CREATE TABLE tmp_148_58 - name of table doesn't contains #. So you may have no right to create real tables under account you've running your sp.

I suggest you to rewrite your code without that confusing dynamics and error should go away ;)

Upvotes: 1

Related Questions