我在SQL Server 2008 R2中要求传递许多行以更新到许多行。我决定尝试使用表值参数来完成此任务。首先 - 我在服务器上创建了类型。
CREATE TYPE [dbo].[crm_question_idx_type] AS TABLE(
[survey_question_id] [int] NOT NULL,
[row_index] [int] NOT NULL,
PRIMARY KEY (survey_question_id)
)
GO
这很好但是在尝试创建存储过程以使用新类型时出现了问题。创建新过程的代码是:
CREATE PROCEDURE dbo.crm_question_index_update
(
@question_idx dbo.crm_question_idx_type READONLY
)
AS
SET NOCOUNT ON
UPDATE crm_survey_question
SET crm_survey_question.row_index = @question_idx.row_index
FROM crm_survey_question
INNER JOIN @question_idx
ON crm_survey_question.survey_question_id = @question_idx.survey_question_id
当我尝试在服务器上编译它时,我收到以下错误:
Msg 137,Level 16,State 1,Procedure crm_question_index_update,Line 10
必须声明标量变量“@question_idx”。
现在我的代码@question_idx
是dbo.crm_question_idx
类型的输入参数,所以我无法理解为什么会弹出这个错误。
答案 0 :(得分:3)
别名是你的朋友。在这种情况下,它们不仅可以简化代码量,而且在引用表变量或TVP时也是必需的。
UPDATE c
SET c.row_index = q.row_index
FROM dbo.crm_survey_question AS c
INNER JOIN @question_idx AS q
ON c.survey_question_id = q.survey_question_id;