简体   繁体   English

SQL Server存储过程错误。 不合法的识别符

[英]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. 名称'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),item_value_notes_college_entry varchar(250),item_value_note_college_entry varchar(250),onge_enter_enter_enter varchar(250), varchar(20))'不是有效的标识符。

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. 我将上述查询从mysql转换为SQL Server。

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 # . 另外,根据您的错误,脚本似乎尝试创建真实的而不是临时的表-请参见CREATE TABLE tmp_148_58表名不包含# So you may have no right to create real tables under account you've running your sp. 因此,您可能无权在运行sp的帐户下创建真实表。

I suggest you to rewrite your code without that confusing dynamics and error should go away ;) 我建议您重写代码,不要使混乱的动态和错误消失;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM