SQL命令无法按预期运行

时间:2014-04-10 13:09:38

标签: c# asp.net sql sql-server web

您正在尝试使用两个表中匹配的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

2 个答案:

答案 0 :(得分:1)

试试这个:

修复您的第一个INSERT:

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上收到错误说明转换错误,您确定UserDmail在UserDetails表上是否为VARCHAR?也许你应该发布那个表结构。

答案 1 :(得分:0)

变量需要切换 从

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

到这个

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