我有一个存储过程,但是当我从我的前端执行它,我得到这个错误: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。
此外,你为什么要创建这样的临时表? – DavidG 2014-09-24 10:48:43
您的代码中根本没有创建/引用表“tmp_148_58”。错误正在从其他地方抛出。请找到相关的代码部分并发布。 – CodeNewbie 2014-09-24 10:54:41