Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Friday, April 9, 2010

Como resolver utilizadores orfãos no Sql Server

Depois de fazer restore a uma base de dados de outra instalação os utilizadores podem ficar orfãos (o nome é o mesmo mas o SID é diferente de instalação para instalação).

Eis como resolver o problema:

-- Quais os utilizadores orfãos?
sp_change_users_login @Action='Report';

-- O 'utilizador' username da base de dados é o utilizador 'username' no sql server:
sp_change_users_login @Action='update_one', @UserNamePattern='username',
@LoginName='username';

Wednesday, March 17, 2010

update with inner join in sql server

You cannot update 2 tables at once (you need 2 statements to do that, one for each statement), but you can update with inner joins. The trick is to use the full table name that you want to update, using the keyword "from" before the "where" but after the "set"


E.g.

update table1 set table1.a = x.a, table1.b = ...
from ...... inner join table99 x on x.id = .... and ...
inner join table1 on table1.id = .........
where x.f in (select f ....) and ...

Just mind the bold lettering, the rest is to show you can build complex queries this way.