2014-09-24 597 views
0

我有一个存储过程,但是当我从我的前端执行它,我得到这个错误:SQL Server存储过程错误。无效标识符

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.

我的程序代码:

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。

+0

此外,你为什么要创建这样的临时表? – DavidG 2014-09-24 10:48:43

+0

您的代码中根本没有创建/引用表“tmp_148_58”。错误正在从其他地方抛出。请找到相关的代码部分并发布。 – CodeNewbie 2014-09-24 10:54:41

回答

1

首先 - 我真的不知道为什么你使用这一切动态创建的报表。正如我从脚本中看到的 - 唯一的原因是您创建临时表的唯一名称。

但是,您并不需要临时表具有唯一的名称,因为此表仅在存储过程的创建范围内可见(并且也位于从该存储过程调用的“子”过程的范围内)。

此外,根据您的错误,它看起来像您的脚本试图创建真实的,而不是临时表 - 请参阅CREATE TABLE tmp_148_58 - 表的名称不包含#。因此,您可能无权在运行sp的帐户下创建真实的表格。

我建议你重写你的代码没有这种混乱的动态和错误应该消失;)

+0

我用我的形式,在我的程序动态控件我串连这样得到临时表名SET @tmpTableName = CONCAT(“#TMP _”,@ collegeId,“_”,@登录ID); – Skumar 2014-09-24 11:04:50

+1

@Skumar,再次 - 没有真正的需要您的临时表名称是唯一的,因此动态创建 – 2014-09-24 11:05:58