sql-server-2008


How to pass User and LogIn as a parameter to a stored procedure?


I have a script that drops the user if it exists and then recreates it. I have this repeated like 30 times. Shown below:
IF EXISTS (SELECT 1 FROM sys.sysusers AS s WHERE s.name = N'johndoe')
BEGIN
PRINT 'Dropping user johndoe...'
DROP USER johndoe;
END
CREATE USER [johndoe] FOR LOGIN [uat\johndoe];
I was wondering if it would be possible to write a stored procedure that accepts user name and domain name and then drops and recreates the user?
CREATE PROCEDURE dbo.usp_DropAndRecreateUsers
(#username varchar(100),
#domainName varchar(100))
AS
DECLARE #loginName varchar(200) = '['+#domainName+'\'+#username+']';
IF EXISTS (SELECT 1 FROM sys.sysusers AS s WHERE s.name = #username)
BEGIN
DROP USER #username;
END
CREATE USER #username FOR LOGIN #loginName;
so that I could call like:
EXEC usp_DropAndRecreateUsers #username='johndoe', #domainName='uat'
However, after writing a stored procedure, I realized it wouldn't be as easy as I thought. Which probably is the reason this wasn't already done. But still, is this even possible to do in SQL Server (2008 and above)?
What I did was (which doesn't work):
CREATE PROCEDURE dbo.usp_DropAndRecreateUsers
(#username varchar(100),
#domainName varchar(100))
AS
DECLARE #loginName varchar(200) = '['+#domainName+'\'+#username+']';
IF EXISTS (SELECT 1 FROM sys.sysusers AS s WHERE s.name = #username)
BEGIN
DROP USER #username;
END
CREATE USER #username FOR LOGIN #loginName;
Rather than running from variable, create query and execute it

Related Links

Flyway Migrate/ ant migrate issue
Can we copy the results of an executed stored procedure data into temp table directly?
Print XML attribute in SQL Server
T-SQL with ORDER BY differences in stored procedure reveal GROUP BY issues
Grails Support to Handle Datasource Failure
MS SQL server user account for ODBC access
Populating columns of one table with rows from another. Based on criteria
How to populate a MSChart form using vb6
SQL Server view partial expansion and performance: in or = filter vs join
SQL Server Pivot Ordering issue
Optimal way to copy mass data between two SQL Server 2008 tables
MS sql use query same as in ms access
Parameter as a result
SqlServer 2008 R2: Displaying first error within TRY/CATCH error management
Sql Query To Create Attendance Report WIth Time Difference
Left Trim 'abc' and right trim 'xyz'

Categories

HOME
json
openssl
nuxeo
google-contacts
laravel-5.3
decorator
avro
computer-vision
search-engine
iis-7.5
sudo
netflix-feign
xamarin-studio
cockroachdb
x-frame-options
rhapsody
android-toolbar
derived
propel2
tortoisegit
multiple-columns
embedly
cronet
fabric
auditing
switching
java-ee-7
branch
rhmap
upnp
xor
helix-3d-toolkit
spring-insight
oscommerce
office365connectors
topic-modeling
active-model-serializers
rule
control-flow-graph
android-maps-v2
upsert
z3py
ajp
istorage
rhel6
ogre3d
ticker
rpostgresql
nuget-server
bettercms
verbose
system.web.ui.webcontrols
windows-azure-pack
pydio
foxit
listadapter
inotifypropertychanged
freefem++
jenkins-scriptler
hyperthreading
sourcegear-vault
subresource-integrity
cpu-cores
gray-code
xaml-designer
mdm-zinc
hg-git
hana-xs
start-job
cocoascript
tcpreplay
dmp
em
java-collections-api
getimagedata
planetary.js
ssms-addin
magic-numbers
dbsetup
dvcs
healthvault
modeshape
astyanax
mstsc
shortcuts
adomd.net
qtgui
process-monitor
angularjs-timeout
moq-3
deploying
point-sprites
emacs23
ou
svn-hooks
osx-leopard
delegatecommand
openvg
system-codedom-compiler
project-lifecycle

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile