开发者

UpdateCommand for Asp GridView using a join between two tables

I have two tables that I join sims and transactionlog and display in a Asp GridView using a SQLDatasource

Then I have an ASP GridView with the following select and update statements:

SelectCommand="SELECT * FROM sims s INNER JOIN TransactionLog AS tl ON s.id = tl.SimsId"     
UpdateCommand="UPDATE SET s.Notes=@Notes FROM sims s INNER JOIN TransactionLog AS tl ON s.id = tl.SimsId WHERE s.id=@id and tl.Id=@Id1"

Right now I only have the Notes column not set to readonly, but would like to add the others in later as soon as I can get the update statement to work for the notes column first.

When I try to update the notes column the error I'm getting is the following: Incorrect syntax near the keyword 'SET'.]

Mmm, hope my question is stated clea开发者_运维问答rly enough.


This is how you write and SQL UPDATE statement with the FROM clause.

drop table t2
drop table t1
go

create table t1 (
Id int not null identity(1,1) primary key,
Name varchar(50) null
)

create table t2 (
t1Id int not null foreign key references t1(id),
Name varchar(50) null
)

insert into t1(name) values('T1Test1')
insert into t1(name) values('T1Test2')
insert into t2(t1Id, name) values(1, 'T2Test1')
insert into t2(t1Id, name) values(2, 'T2Test2')

-- Have a look at the data
select * from t1 inner join t2 on t1.Id = t2.t1Id

update t2 
set t2.Name = 'T2Test1_changed'
from t2 inner join t1 on t2.t1Id = t1.Id
where t1.Id = 1

-- See the changes
select * from t1 inner join t2 on t1.Id = t2.t1Id

This means that your statement should look like this:

UPDATE sims
SET s.Notes = @Notes 
FROM sims s INNER JOIN TransactionLog tl ON s.id = tl.SimsId 
WHERE s.id = @id and tl.Id = @Id1

I do however doubt the WHERE clause. Maybe there's no need to use both IDs there, but only you know that.

Try you queries in SQL Management Studio before applying them in you application for a better debugging experience.

Here is the MSDN docs on the UPDATE statement: http://msdn.microsoft.com/en-us/library/ms177523.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜