I checked past posts and could not find what I was looking for - or could not understand it well enough to implement. I am fairly new at T-SQL and am having issues with the following dynamic sql statement:
DECLARE @sql nvarchar(60)
DECLARE @strSQL nvarchar(50)
DECLARE @login nvarchar(50)
DECLARE @id nvarchar(50)
SET @id = 'master';
SET @login = 'OP\mickey.mouse';
SELECT @strSQL = ' USE ' + @id + ';' + 'SELECT * FROM sys.server_principals WHERE name = + @login'
EXEC sp_executesql @strSQL, N'@login nvarchar(50), @id nvarchar (50)', @login, @id
The query executes without error however it returns every row from the sys.server_princpals table. If I run a simple select stmt. using the same where clause, I only get the one row I am looking for. Eventually I will be rolling this sql into a stored proc
that will accept a login as an input arg and go look for the specified user in all of the databases - for now though I need to get this to work. Any help is appreciated.
View Complete Post