SQL命令未按预期运行

本文关键字:运行 命令 SQL | 更新日期: 2023-09-27 17:57:41

嗨,我正试图使用两个表中匹配的id,用来自另一个表的电子邮件更新一个表。我一定做错了,因为我收到了这些错误

"Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'IN'.
Msg 1087, Level 15, State 2, Line 10
Must declare the table variable "@ListofPropIDs".
Msg 1087, Level 15, State 2, Line 11
Must declare the table variable "@ListofPropIDs"."

如有任何帮助,我们将不胜感激。

DECLARE @ListOfUserIdsAndEmails TABLE(UserEmail VARCHAR(100),IDs VARCHAR(100));
INSERT INTO         @ListOfUserIdsAndEmails 
    SELECT              UserId, Username
    FROM                aspnet_Users
    UPDATE          UserDetails
    SET             UserEmail = IN ( SELECT UserEmail from @ListofPropIDs)
    WHERE           UserIdn   = IN ( SELECT IDs from @ListofPropIDs)
GO

SQL命令未按预期运行

试试这个:

修复您的第一次插入:

INSERT INTO         @ListOfUserIdsAndEmails 
SELECT              Username, UserId
FROM                aspnet_Users

UPDATE          UD
SET             UserEmail = P.UserEmail 
FROM UserDetails AS UD
JOIN @ListOfUserIdsAndEmailsAS P
  ON P.IDs = UD.USerIdn

但是,如果你在UserEmail上收到一个错误,说转换错误,你确定UserDetails表上的UserEmail是VARCHAR吗?也许你应该发布表格结构

变量需要切换来自

DECLARE @ListOfUserIdsAndEmails TABLE( UserEmail nvarchar(256),IDs uniqueidentifier);

到这个

DECLARE @ListOfUserIdsAndEmails TABLE(IDs uniqueidentifier, UserEmail nvarchar(256));