Hello,
My company uses a custom application which utilizes multiple (over 12) databases. Some of the columns common to all said databases are:
UserID, UserName
UserID is a unique field as it contains an active directory ID.
UserName contains user's first and last name(s).
Example contents of UserID could be BS9870
Example contents of UserName could be Bob Smith
UserName field may not be unique; there may be multiple
John Smith's for example.
One of my tasks is to search all of the databases to see if a user is present there. Currently, we use a query like the following:
Select UserID, UserName as UserName_From_Northdb
From Northdb
Where UserID like '%BS9870%' or UserName like ''%Smith%'
Select UserID, UserName as UserName_From_Southdb
From Southdb
Where UserID like '%BS9870%' or UserName like ''%Smith%'
Select UserID, UserName as UserName_From_Eastdb
From Eastdb
Where UserID like '%BS9870%' or UserName like ''%Smith%'
etc.
There is one of these 3 line queries per each database. All the queries are ran at the same time.
Is there a way to create two global search variables and do something like the following:
At the top of the script enter in a variable for UserID and a variable for UserName then have the script search each database for that variable? The desired state would be entering in the user's UserID and UserName once at the top of the script and having it propagate to all the relevant queries.
Currently, find and replace (within the menu system of SSMS 2012) is being used to find and replace all instances of %BS9870% with %Active Directory ID of user being searched for%'. Another find and replace is used to replace %Smith% with %Last name of user being searched for%.
Note:
I need to know which database the user is found in and do not want to run each of these 3 line queries one by one, which is why UserName has an alias which includes the database name.
There is likely a much better way to write this script.
Thanks for the advice.