Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

update one table from another when the column names match

$
0
0
declare @people table(
	avatarkey varchar(36) not null primary key, 
	avatarname varchar(50),
	prims int,
	allotment int,
	lockers int
)

--lots of code to fill table

update @people set prims=(select prims from PrimOwners where PrimOwners.avatarkey=@people.avatarkey)

However I get...

Must declare the scalar variable "@people".

so normally I would use table aliases like this:

update @people P set P.prims=(select O.prims from PrimOwners O where O.avatarkey = P.avatarkey)

...But SQL Server does not allow aliases in an update like this.

Incorrect syntax near 'P'.

Note that it WORKS if the column names do not match.

declare @people table(avatarkey1 varchar(36) not null primary key, 
	avatarname varchar(50),
	prims int,
	allotment int,
	lockers int
)

-- fill table

update @people set prims=(select prims from PrimOwners where avatarkey = avatarkey1)
So what am I missing here?  I'd rather not have to rename my column, as I would have to go through my whole app and change it everywhere.


I'd rather live with false hope than with false despair.


Viewing all articles
Browse latest Browse all 23857

Trending Articles