9

我试图从相同的表数据更新相同的表数据。

我的SP如下图:

UPDATE T1
SET T1.Name = T2.Name
   , T1.Age = T2.Age
   , T1.Subject = T2.Subject
FROM Student T1
   , Student T2
WHERE T1.StudentID = @OldID
   AND T2.StudentID = @NewID

当我执行这个查询时,没有错误。但更新不起作用。

[这里没有要比较的通用列值,例如 T1.StudentID = T2.StudentID]

4

3 回答 3

5

尝试这个:

UPDATE t1
SET t1.name = t2.name, t1.age = t2.age, t1.subject = t2.subject
FROM student t1
INNER JOIN
student t2
ON t1.StudentID = @oldID
AND t2.StudentID = @NewID

完整的例子在这里

于 2012-10-11T14:48:32.213 回答
2

试试这个,你使用了错误的别名。

UPDATE T1 SET Name = T2.Name
, Age = T2.Age
, Subject = T2.Subject
 FROM Student T1
, Student T2
WHERE T1.StudentID = @OldID
AND T2.StudentID = @NewID
于 2012-10-27T11:13:45.420 回答
0

尝试通过存储过程来完成,声明一些变量,将新值加载到这些变量中,然后更新您的表。

应该看起来像这样:

CREATE PROCEDURE dbo.StoredProcedure2

    @OldID int,
    @NewID int

AS

declare @Name text, @Age int, @Subject text

begin

select @Name = T1.Name, @Age = T1.Age, @Subject= T1.Subject
from Student T1
Where T1.StudentID = @OldID

end

begin

UPDATE T1 SET T1.Name = @Name, T1.Age = @Age, T1.Subject = @Subject
FROM Student T1
WHERE T1.StudentID = @OldID

end
    RETURN
于 2012-10-15T19:46:07.290 回答