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

时间:2014-09-24 10:42:13

标签: sql sql-server

我有一个存储过程,但是当我从前端执行它时,我收到此错误:

  

名称' 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))'不是一个   有效的标识符。

我的程序代码:

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

任何人都可以帮助我解决它吗?我将上面的查询从mysql转换为SQL Server。

1 个答案:

答案 0 :(得分:1)

首先 - 我真的想知道为什么要使用所有这些动态创建的语句。正如我从您的脚本中看到的 - 唯一的原因是它正在创建的临时表的唯一名称。

但是你真的不需要你的临时表具有唯一的名称,因为这个表只在创建它的存储过程的范围内可见(并且也在那个从那个调用的'子'程序的范围内)

此外,根据您的错误,您的脚本似乎尝试创建真实而非临时表 - 请参阅CREATE TABLE tmp_148_58 - 表名称不包含#。因此,您可能无权在运行sp。

的帐户下创建真实表格

我建议你重写你的代码而不要混淆动态,错误应该消失;)