how can I do this select query - sql

I have in my SQL Server, a table called FavoriteUsers with a list of users and a table called Users with all users.
In my ASPX page, I have a TextBox where my user can put the ID of an User to list in my GridView.
How it works
By default, I fill my GridView with all data of my FavoriteUsers table, and if the user of my system put some user code in the textbox field and click on my button to find these users, I'll reload the GridView with the FavoriteUsers and the users that he put on TextBox field that come from the table Users.
How can I do this SELECT query ?

select * from FavoriteUsers
UNION
select * from Users
Where userid = 'TextBox.text'
Of course you can't use TextBox.text in there. But you only needed the query so you get the point.
NOTE: The UNION will leave out any records from Users that already exist in FavoriteUsers. So you won't have duplicates.

Get all favorite users plus the user whose username matches that entered into the textbox:
SELECT Users.*
FROM Users
LEFT JOIN FavoriteUsers ON Users.UserID = FavoriteUsers.UserID
WHERE (FavoriteUsers.UserID IS NOT NULL) OR (Users.Username = #Username)

Related

Query Salesforce users and their permission sets (SOQL)

i'm trying to run an SOQL query on salesforce to get all users and their permission set id.
it is important the list return will be by user not by permission sets, meaning if i have 1000 users i will get back 1000 records and for each record the user attributes like email etc + permission sets list of Id's assign to him
SELECT+id,PermissionSet.id+FROM+User i tried finding the relationship field name but i'm not so familiar wtih salesforce, please assist
https://developer.salesforce.com/docs/atlas.en-us.238.0.object_reference.meta/object_reference/sforce_api_erd_profile_permissions.htm
The table you're looking for is PermissionSetAssignment
Top-down:
select id, email,
(select permissionsetid
from permissionsetassignments
where permissionset.isownedbyprofile = false)
from user
or bottom-up
select assigneeid, assignee.email, permissionsetid
from permissionsetassignment
where permissionset.isownedbyprofile = false
order by assigneeid

How to display different interactive grid queries, depending on user privileges

I use Master-Detail page. I want to display whole table only for user who is an administrator. For the user who isn't the administrator, I want to display sql query which will be restrict MD view.
I tried to create another one master detail on same page which is visible only for users without Administrator role. And first MD is not visible for them.(I used Server-side Condition) Is there exist some other way to display different query, depending on user role.
I hope I explained the problem clearly. Thanks in advance
Have a look at the package APEX_ACL, you can use the related views in your where clause.
Example:admins see all rows, other users only see the row for KING
SELECT *
FROM emp
WHERE
-- user is admin ?
( EXISTS ( SELECT 1
FROM apex_appl_acl_user_roles
WHERE application_id = :APP_ID AND
user_name = :APP_USER AND
role_static_id = 'ADMINISTRATOR'
) ) OR
-- user is no admin
( ename = 'KING' );

How to update a field that is a drop down list from another table using a SQL query

Hey guys in my access database I have two tables labeled 'Contacts' and 'StatusList'. StatusList consists of one column and four rows labeled:
StatusDescriptionSuspectProspectInquiryApplicant
Contacts consists of the following:
First Last Email Status Phone
The Status field in Contacts is a drop down box that feeds of off the StatusList table. Every row in the Contacts table is set to Suspect initially.
The email field in Contacts is set to only allow unique emails.
I have a table labeled ExcelImport that has the same fields as the Contacts table.
Eventually, I want to insert the the data from my ExcelImport table to Contacts. If there is an email match between ExcelImport and Contacts, I want the row in Contacts for the Status field to be updated from Suspect to Prospect.
So far I have this:
SELECT Contacts.contactEmail
FROM Contacts
Inner Join ExcelImport on Contacts.contactEmail = ExcelImport.contactEmail;
I know this only shows me the emails that match. Would there be any way to update the Status list for that specific row that matched to Prospect ?
As mentioned, simply change the value with an UPDATE action query and the dropdown will change accordingly. The dropdown if I understand you is simply a lookup combox box that you possibly set in the Table Design. It simply helps control the values entered. The UPDATE query will change that dropdown as value of table changes:
UPDATE Contacts INNER JOIN ExcelImport
ON Contacts.contactEmail = ExcelImport.contactEmail
SET Contacts.[Status] = 'Prospect';
Additionally, you mention I want to insert the the data from my ExcelImport table to Contacts which you can do so preserving the uniqueness of the Email field by running a NOT EXISTS
clause in append query.
INSERT INTO Contacts ([First], [Last], [Email]], [Status], [Phone])
SELECT e.[First], e.[Last], e.[Email]], e.[Status], e.[Phone]
FROM ExcelImport e
WHERE NOT EXISTS
(SELECT 1 FROM Contacts c WHERE c.[Email] = e.[Email])
Therefore, run both action queries to import unique Email and for repeated Email, update Status.

SQL Server query for a VB dropdown list

I am working on administering an "erp type" VB program which is running on SQL Server 2008 R2. This program allows users to have a card for each file stored in its vault. Those cards are built into administration tools and u can add elements like comboboxes, dropdownlists etc. You can also add an SQL query into a dropdown list. So i have these 2 tables in SQL Server database:
USERS:
USERID / NAME / USERGROUPID
and
USERGROUPS:
USERGROUPID / USERGROUPNAME
I want to add 2 dropdown lists in my cards connected to SQL with query but i want the 2nd one to be connected with the selection from the 1st one. The 1st one asks for the usergroup name and the 2nd one asks for the name in the previously selected usergroup.
I found out that the 1st dropdown list should be:
SELECT USERGROUPNAME
FROM USERGROUPS
The 2nd one should be like:
SELECT NAME
FROM USERS
INNER JOIN USERGROUPS ON USERS.USERGROUPSID = (USERGROUPS.USERGROUPID FROM PREVIOUS SELECTION)
How can I modify the 2nd query in order to get the data I need?
You can use the following query for second one. It will select all NAME that matches with the GroupName you selected
SELECT NAME
FROM USERS usrs
INNER JOIN USERGROUPS usrgrps ON usrs.USERGROUPSID=usrgrps.USERGROUPID
WHERE usrgrps.USERGROUPNAME='VALUE FROM DROPDOWN 1';

Dropdown list validation based on another field's value in SQL

I currently have the following 4 tables:
Contacts
Contact_ID,
ContactName
Roles
Role_ID,
Role
Contact Roles
ContactRole_ID,
Contact_ID,
Role_ID
Events - Contacts
EventContact_ID
Contact_ID
ContactRole_ID
My end goal is to limit the value that can be selected in the ContactRole_ID field of the Events - Contacts table based on the value selected in the Contact_ID field so that only Roles that the Contact is associated with will show up as options. Is there a way to do that using SQL?
If any additional information or clarification is needed, please let me know. Thank you in advance for any help.
Yes, we can do this using inner join with Contact Roles table and Events_Contacts table. Based on the column Contact_ID which is available in both the tables.
SELECT EC.[EventContact_ID],EC.[Contact_ID],EC.[CotactRole_ID]
FROM Contact_Roles CR INNER JOIN Events_Contacts EC ON
CR.Contact_ID=EC.Contact_ID
If I am right, for creating ms access form filter drop down you trying to do this??
If not, oops.. If you want to know more things based on this requirement in ms access, let me know. I will guide you.
The recordsource of a dropdown list filtered by a another control value should look like this.
The "another control" can be a text box or a dropdown list also.
SELECT Field1, Field2, ...etc
FROM table1 (JOIN more tables here if you want)
WHERE Field3 = Forms!Form_Name.Controls!Control1_Name
AND Field4 = Forms!Form_Name.Controls!Control2_Name(.value if required)
...
etc
Or you can save this as a query and use it where you need. Read this article, it will help you a lot. This Access feature is very useful.