更新选择具有多个联接的语句

时间:2011-10-06 21:29:55

标签: sql-server-2005 join sql-update

我正在尝试根据Contacts_CSTM表中的数据更新Project_CSTM表中的数据。这是我正在使用的查询,但是我收到一个错误:“从字符串转换为uniqueidentifier时转换失败”

 ALTER PROCEDURE Insurance_Check_Expiration
  @ID_C AS NVARCHAR (55) = ID_C
  AS
 BEGIN      
 SET NOCOUNT ON
 IF EXISTS(SELECT * FROM CONTACTS_CSTM WHERE ID_C = @ID_c)

 Update contacts_cstm set insurance_expired_label_c = 'INSURANCE EXPIRED'
 WHERE DRIVERS_LICENSE_NUMBER_C IS NOT NULL AND @ID_C=
 (SELECT cc.id_c
 FROM PROJECT_CSTM PC 
 JOIN PROJECT P 
 ON P.ID = PC.ID_C
 JOIN PROJECT_RELATION PR
 ON PR.PROJECT_ID = P.ID  
 JOIN CONTACTS C
 ON C.ID = PR.RELATION_ID 
 JOIN CONTACTS_CSTM CC
 ON CC.ID_C = C.ID
 WHERE CC.ID_C = @ID_C AND INSURANCE_EXPIRED_C ='1') 

感谢。

1 个答案:

答案 0 :(得分:1)

首先,您将@ID_C的值设置为值(ID_C),这显然不是有效的GUID。

这与你所做的功能相同,运行它并且你会得到同样的错误。

CREATE PROCEDURE Insurance_Check_Expiration
  @ID_C AS NVARCHAR (55) = ID_C
  AS
 BEGIN      
 DECLARE @A UNIQUEIDENTIFIER

 SET @A = @ID_C


 END

 exec Insurance_Check_Expiration

编辑:这是一个基于OP评论的功能性示例:

CREATE TABLE GUIDExample (ID_C UNIQUEIDENTIFIER)
GO
CREATE PROC GuidExample_Insert @ID_C UNIQUEIDENTIFIER
AS
BEGIN
    SELECT @ID_C
END
GO
CREATE TRIGGER GUID_Example ON GUIDExample
AFTER INSERT
AS
    DECLARE @ID_C UNIQUEIDENTIFIER

    SELECT @ID_C = ID_C
    FROM Inserted


    EXEC GuidExample_Insert @ID_C
GO

DECLARE @SampleGUID UNIQUEIDENTIFIER

SET @SampleGUID = NEWID()

INSERT GUIDExample (ID_C)
VALUES(@SampleGUID)