具有多个连接的 UPDATE Select 语句

发布于 2024-12-08 13:53:30 字数 737 浏览 0 评论 0原文

我正在尝试根据 Project_CSTM 表中的数据更新 Contacts_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') 

谢谢。

I am trying to update data in a Contacts_CSTM table based on data in a Project_CSTM table. This is the query I'm using, but I get an error: "Conversion failed when converting from a character string to 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') 

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2024-12-15 13:53:30

其一,您将 @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)

For one, you're setting the value of @ID_C to a value (ID_C) which is obviously not a valid GUID.

This is the functional equivalent of what you did, run it and you'll get the same error.

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

EDIT: Here's a functional example based on the OP's comments:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文