0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQL serverでユーザーをつくろうとしてハマった話(ログインとユーザーの違い、マッピングについて)

Last updated at Posted at 2023-09-07

やりたかったこと

自動化でDBから必要な情報をselectしてSQL内で加工するだけなので、
①Power Automate用のユーザ作成
②db_datareader、db_denydatawriterの付与(読み取り専用を明示的に)

こんなん5分で終わるやろ、て思ったら午前中を丸々つぶしたのは内緒です

最初に参考にしたもの

この後ドツボにはまりますが、自分が無知なだけでこちらの記事を批判とかそういう意図は無いです。
https://www.kwbtblog.com/entry/2019/10/11/030233

もしストアドプロシージャやファンクションの実行が必要な人は
こちらの記事をお読みください。
https://qiita.com/kazumatsukazu/items/9935d524f53c347b0e3c

新しく作ったユーザでログインしようとするとエラーが出る

image.png

SQL Serverの2種類のユーザー

https://sql-oracle.com/sqlserver/?p=143
なるほど、SQL serverとDBでそれぞれ設定が必要だった模様
下記のスクショには確かにServer側に使いたいログインが無い(DB側にはユーザーとしてある)
image.png

Server側のログインも作成したが。。。

ServerログインとDBユーザーが紐づいてないもんね、そりゃそうだ。
image.png

マッピングしたいのに、みんなが出てくるUIウィンドウの代わりにT-SQLの画面出るのなぁぜなぁぜ?

顧客がほしいもの
https://blog.itparadise.jp/?p=575
image.png
提案されたもの
image.png
くそぅ

ひたすらググる

ちなみに調べてると出てくる

EXEC sp_change_users_login

ですが、将来削除予定で今は非推奨のようです(自分の環境ではエラー出ました。そんなSP無いぞ、と)。

--Map database user MB-Sales to login MaryB.  
USE <db_name>;  
GO  
ALTER USER <USERNAME> WITH LOGIN = <LOGINNAME>; 
GO  

でもエラー

The user cannot be remapped to a login. Remapping can only be done for users that were mapped to Windows or SQL logins.

なんかユーザー作成の地雷全部踏み抜いてないか?

今度こそ

ユーザー作るときにfor でログインを指定しないといけなかったみたい。

create user <user-name> for login <login-name>

てなわけで既存のユーザー削除して新規作成。

それでもエラーが出たのでユーザが存在するDB名を指定するようにしたらログイン出来ました!!!!!!!
https://azureops.org/articles/the-server-principal-is-not-able-to-access-the-database-master-under-the-current-security-context/
image.png

教訓

きちんと基礎から学ぼうな

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?