方案
我创建了一个商店程序,我将用户定义的表作为参数,然后首先检查Role_ID
和Form_ID
是否存在然后我会更新我现有的表格。否则我将插入现有表格。
问题
在我的商店程序中写入插入查询后,我在改变商店程序时遇到了以下错误。
消息137,级别16,状态1,过程SP_SETUP_ROLES_RIGHTS_SAVE_AND_UPDATE,第17行 必须声明标量变量" @ Temp"。 消息137,级别16,状态1,过程SP_SETUP_ROLES_RIGHTS_SAVE_AND_UPDATE,第17行 必须声明标量变量" @ Temp"。
以下是我的SP代码:
CREATE TYPE _ROLERIGHTSSCHEMA as Table (
_Role_ID int,
_Form_ID int,
_Form_Name varchar(100),
_Can_View bit,
_Can_Edit bit,
_Can_Prepare_By bit,
_Can_Change_Status_By bit,
_Prepared_By_ID int,
_Prepared_Date datetime
)
ALTER PROCEDURE SP_SETUP_ROLES_RIGHTS_SAVE_AND_UPDATE
(@Temp _ROLERIGHTSSCHEMA ReadOnly)
AS
BEGIN
IF EXISTS(SELECT * FROM ROLE_RIGHTS WHERE Role_ID = @Temp.Role_ID AND Form_ID = @Temp.Form_ID)
BEGIN
Update Role_Rights
set Can_View = t._Can_View,
Can_Edit = t._Can_Edit,
Can_Prepare_By = t._Can_Prepare_By,
Can_Change_Status_By = _Can_Change_Status_By,
Modified_By_ID = 0,
Modified_Date = GETDATE()
From @Temp t
WHERE Role_Rights.Role_ID = t._Role_ID and Role_Rights.Form_ID = t._Form_ID
END
ELSE
BEGIN
INSERT INTO Role_Rights (
Role_ID,
Form_ID,
Can_View,
Can_Edit,
Can_Prepare_By,
Can_Change_Status_By,
Prepared_By_ID,
Prepared_Date
) SELECT _Role_ID, _Form_ID, _Can_View, _Can_Edit, _Can_Prepare_By, _Can_Change_Status_By, 0, GETDATE() FROM @Temp
END
END
答案 0 :(得分:1)
要一次更新/插入多条记录,请使用MERGE
:
ALTER PROCEDURE SP_SETUP_ROLES_RIGHTS_SAVE_AND_UPDATE
(@Temp _ROLERIGHTSSCHEMA ReadOnly)
AS
BEGIN
MERGE Role_Rights rr
USING @Temp t
ON rr.Role_ID = t._Role_ID
and rr.Form_ID = t._Form_ID
WHEN MATCHED THEN
Update set Can_View = t._Can_View,
Can_Edit = t._Can_Edit,
Can_Prepare_By = t._Can_Prepare_By,
Can_Change_Status_By = _Can_Change_Status_By,
Modified_By_ID = 0,
Modified_Date = GETDATE()
WHEN NOT MATCHED BY TARGET THEN
INSERT ( Role_ID,
Form_ID,
Can_View,
Can_Edit,
Can_Prepare_By,
Can_Change_Status_By,
Prepared_By_ID,
Prepared_Date)
VALUES (_Role_ID, _Form_ID, _Can_View, _Can_Edit,
_Can_Prepare_By, _Can_Change_Status_By, 0, GETDATE() );
END;
修改强>
MERGE
超过IF EXISTS/UPDATE/INSERT set based
:
Single insert -- no difference
Single update -- no difference
Multiple insert -- no difference
Multiple update -- no difference
Multiple insert/update: MERGE -- will handle it correctly
IF EXISTS -- you will lose records to insert
没有IF部分的更新/插入:
Update Role_Rights
set Can_View = t._Can_View,
Can_Edit = t._Can_Edit,
Can_Prepare_By = t._Can_Prepare_By,
Can_Change_Status_By = _Can_Change_Status_By,
Modified_By_ID = 0,
Modified_Date = GETDATE()
From @Temp t
WHERE Role_Rights.Role_ID = t._Role_ID and Role_Rights.Form_ID = t._Form_ID;
INSERT INTO Role_Rights (
Role_ID,
Form_ID,
Can_View,
Can_Edit,
Can_Prepare_By,
Can_Change_Status_By,
Prepared_By_ID,
Prepared_Date
)
SELECT _Role_ID, _Form_ID, _Can_View, _Can_Edit,
_Can_Prepare_By, _Can_Change_Status_By, 0, GETDATE()
FROM @Temp t
WHERE NOT EXISTS (SELECT 1
FROM Role_Rights rr
WHERE rr.Role_ID = t._Role_ID
and rr.Form_ID = t._Form_ID);
答案 1 :(得分:1)
您的exists子句中的语句未正确形成 - 这就是抛出错误。你有:
IF EXISTS(SELECT * FROM ROLE_RIGHTS WHERE Role_ID = @Temp.Role_ID AND Form_ID = @Temp.Form_ID)
但它应该是:
if exists (select * from ROLE_RIGHTS as rr inner join @Temp as tp on rr.Role_ID = tp._Role_ID and rr.Form_ID = tp._Form_ID)
这种纠正毫无意义 - 正如@ lad2025已经提到过的那样。
答案 2 :(得分:0)
在ELSE之后的语句中尝试使用多个IF子句,检查是否有效
CREATE TYPE _ROLERIGHTSSCHEMA as Table (
_Role_ID int,
_Form_ID int,
_Form_Name varchar(100),
_Can_View bit,
_Can_Edit bit,
_Can_Prepare_By bit,
_Can_Change_Status_By bit,
_Prepared_By_ID int,
_Prepared_Date datetime
)
ALTER PROCEDURE SP_SETUP_ROLES_RIGHTS_SAVE_AND_UPDATE
(@Temp _ROLERIGHTSSCHEMA ReadOnly)
AS
BEGIN
IF EXISTS(SELECT * FROM ROLE_RIGHTS WHERE Role_ID = @Temp.Role_ID AND Form_ID = @Temp.Form_ID)
BEGIN
Update Role_Rights
set Can_View = t._Can_View,
Can_Edit = t._Can_Edit,
Can_Prepare_By = t._Can_Prepare_By,
Can_Change_Status_By = _Can_Change_Status_By,
Modified_By_ID = 0,
Modified_Date = GETDATE()
From @Temp t
WHERE Role_Rights.Role_ID = t._Role_ID and Role_Rights.Form_ID = t._Form_ID
END
ELSE
BEGIN
IF NOT EXISTS(SELECT * FROM ROLE_RIGHTS WHERE Role_ID = @Temp.Role_ID AND Form_ID = @Temp.Form_ID)
BEGIN
INSERT INTO Role_Rights (
Role_ID,
Form_ID,
Can_View,
Can_Edit,
Can_Prepare_By,
Can_Change_Status_By,
Prepared_By_ID,
Prepared_Date
) SELECT _Role_ID, _Form_ID, _Can_View, _Can_Edit, _Can_Prepare_By, _Can_Change_Status_By, 0, GETDATE() FROM @Temp
END
END
END