Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Query 2

Status
Not open for further replies.

almaler

Programmer
Nov 24, 2003
48
US
I need to pull information out of a SQL db using a query.
I need to generate a final status of "Locked"or "Unlocked" for each computer in our organization. In order to find out whether the PC is locked down, I have to evaluate what is contained in the SMX_Local_Admins_DATA.Account00 field for each PC. This is a one to many relationship. For each PC, there an be multiple records. So, for each PC if the Account00 field contains a record that is other than "desktop", "security", or "admin" then that PC is unlocked else the PC is locked. I cannot figure out how to write this in SQL. So far, I have tried the following code, but it is not giving me the desired result. I suspect that I might need a variable to hold the value of "Locked" or "Unlocked", but I am not sure.

SELECT
count("Computer_System_Data"."Name0") as Cnt, "Computer_System_Data"."Name0"
FROM
"Computer_System_Data" "Computer_System_Data", "SMX_Local_Admins_DATA" "SMX_Local_Admins_DATA"
WHERE
right("SMX_Local_Admins_DATA"."Account00",len("SMX_Local_Admins_DATA"."Account00")-{fn locate('root\',"SMX_Local_Admins_DATA"."Account00")} +1) not in ('admin','desktop','security')
Group by
"Computer_System_Data"."Name0"




 
Can you provide us the schema (tables and columns) with sample data and the result you wish to see?

-SQLBill

Posting advice: FAQ481-4875
 
Sure! Here is an example of one computer named WSPAULSA.
So this computer has four accounts that are in the administrators group. Only the first three accounts are authorized (desktop, security, admin). So, since this computer has an account other than those three (scott), I would want this computer to be returned as "Unlocked". If this computer only had the three authorized accounts, then I would want this computer to be returned as "Locked".

TABLE: Computer_System_Data
Column: Name0
===============
WSPAULSA


TABLE: SMX_LOCAL_Admins_DATA
Column: Account0
=================
\\WSPAULSA\root\cimv2:Win32_Group.Domain="NEO",Name="Security"
\\WSPAULSA\root\cimv2:Win32_Group.Domain="NEO",Name="desktop"
\\WSPAULSA\root\cimv2:Win32_Group.Domain="NEO",Name="admin"
\\WSPAULSA\root\cimv2:Win32_Group.Domain="NEO",Name="scott"

In the end, I would like the results to look like this:

Computer Name Status
============== ======
WSPAULSA Unlocked
WSPAULSJ Locked
....etc



 
Untested....this should give you the idea...
Code:
SELECT CSD.Name0,
       SUBSTRING(SMXLAD.Account0, (PATINDEX(%NAME=%, SMXLAD.Account0)+5), (LEN(SMXLAD.Account0)))
  FROM Computer_System_Data CSD
    LEFT OUTER JOIN SMX_LOCAL_Admins_DATA SMXLAD
      ON CSD.????  = SMXLAD.????
  WHERE SUBSTRING(SMXLAD.Account0, (PATINDEX(%NAME=%, SMXLAD.Account0)+5), (LEN(SMXLAD.Account0))) NOT IN ('Security', 'admin', 'desktop')

That may not be the most efficient, but TEST it and see if it does the job.

I'll see if I can come up with something better, but if I can't, I'm sure someone else will.

-SQLBill

Posting advice: FAQ481-4875
 
Okay, I overlooked some important facts...
Code:
SELECT CSD.Name0,
       STATUS = CASE WHEN (SUBSTRING(SMXLAD.Account0, (PATINDEX(%NAME=%, SMXLAD.Account0)+5), (LEN(SMXLAD.Account0)))) NOT IN ('Security', 'admin', 'desktop')
   THEN 'UNLOCKED'
   ELSE 'LOCKED'
 FROM Computer_System_Data CSD
    LEFT OUTER JOIN SMX_LOCAL_Admins_DATA SMXLAD
      ON CSD.????  = SMXLAD.????

But you still need to be able to join the tables on some common field. That's what the ???? are for.

-SQLBill

Posting advice: FAQ481-4875
 
If you don't have columns in common, a REALLY inefficient join would be:
Code:
 ON CSD.Name0 = SUBSTRING(SMXLAD.Account0, 3, 8)

-SQLBill

Posting advice: FAQ481-4875
 
I forgot to mention that the join should be on the MachineID field. I did test this and I got an error that says:
"Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'NAME'."


SELECT CSD.Name0,
STATUS = CASE WHEN (SUBSTRING(SMXLAD.Account0, (PATINDEX(%NAME=%, SMXLAD.Account0)+5), (LEN(SMXLAD.Account0)))) NOT IN ('Security', 'admin', 'desktop')
THEN 'UNLOCKED'
ELSE 'LOCKED'
FROM Computer_System_Data CSD
LEFT OUTER JOIN SMX_LOCAL_Admins_DATA SMXLAD
ON CSD.MachineID = SMXLAD.MachineID
 
Ok, I looked up PATINDEX and found that I needed single quotes around %NAME%, so I changed it to the below query, but now I am getting this error:

Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FROM'.


SELECT CSD.Name0,
STATUS = CASE WHEN (SUBSTRING(SMXLAD.Account0, (PATINDEX('%NAME=%', SMXLAD.Account0)+5), (LEN(SMXLAD.Account0)))) NOT IN ('Security', 'admin', 'desktop')
THEN 'UNLOCKED'
ELSE 'LOCKED'
FROM Computer_System_Data CSD
LEFT OUTER JOIN SMX_LOCAL_Admins_DATA SMXLAD
ON CSD.MachineID = SMXLAD.MachineID
 
SELECT CSD.Name0,
STATUS = CASE WHEN (SUBSTRING(SMXLAD.Account0, (PATINDEX('%NAME=%', SMXLAD.Account0)+5), (LEN(SMXLAD.Account0)))) NOT IN ('Security', 'admin', 'desktop')
THEN 'UNLOCKED'
ELSE 'LOCKED' END as ColumnNameHere
FROM Computer_System_Data CSD
LEFT OUTER JOIN SMX_LOCAL_Admins_DATA SMXLAD
ON CSD.MachineID = SMXLAD.MachineID

Denis The SQL Menace
SQL blog:
Personal Blog:
 
SQLDenis,

I got this error:
Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'as'.
 
I took out the AS clause and it worked beautifully!!!! Thank you SQLBill and SQLDenis!!!
 
OK, upon further inspection of the results set, it is not working exactly right.

Machine names show up more multiple times and all but 6 machines show to be "Unlocked" which is definitely not the case.


Any ideas?
 
Ok - it is working. I had to put in a quote and change the position to +6 since the account names are surrounded in quotation marks. This works!! Thank you!!!!

SELECT CSD.Name0, SMXLAD.Account00,
STATUS = CASE WHEN (SUBSTRING(SMXLAD.Account00, (PATINDEX('%NAME=%', SMXLAD.Account00)+6), (LEN(SMXLAD.Account00))))
NOT IN ('aacsecurity"', 'pcadmin"', 'aac desktopguys"')
THEN 'UNLOCKED'
ELSE 'LOCKED' END
FROM Computer_System_Data CSD
LEFT OUTER JOIN SMX_LOCAL_Admins_DATA SMXLAD
ON CSD.MachineID = SMXLAD.MachineID

 
Glad we could help, thanks for the star.

-SQLBill

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top