简体   繁体   English

SQL Server Management Studio - 将包含软件名称和版本的列添加到具有特定软件的系统的 SQL 查询

[英]SQL Server Management Studio - Add columns containing software name and version to SQL Query for systems with specific software

I am using Microsoft SQL Server Management Studio to query for computers with a specific software in SQL Server database.我正在使用 Microsoft SQL Server Management Studio 在 SQL Server 数据库中查询具有特定软件的计算机。 Unfortunately, I am completely new to SQL so I am trying to learn on the fly.不幸的是,我对 SQL 完全陌生,所以我正在努力学习。 I was able to create a basic query thanks to help on Prajwal's site .感谢Prajwal 网站帮助,我能够创建一个基本查询。

However I would like to refine it a little bit.不过我想稍微改进一下。 I want to have two extra columns which display the software name, and the software version.我想要两个额外的列来显示软件名称和软件版本。

To start, here is my code so far:首先,这是我目前的代码:

SELECT DISTINCT
    sys.Netbios_Name0 AS [Computer Name],
    sys.User_Name0 AS [User Name]
FROM
    v_R_System sys
JOIN 
    v_Add_Remove_Programs arp ON sys.ResourceID = arp.ResourceID
WHERE
    sys.ResourceID IN (SELECT sys.ResourceID
                       FROM v_R_System sys
                       JOIN v_Add_Remove_Programs arp ON sys.ResourceID = arp.ResourceID
                       WHERE DisplayName0 LIKE '%Adobe Reader%')

This returns the following output:这将返回以下输出:

Computer Name | User Name
--------------+-----------
COMPUTER001   | Jane Doe  
COMPUTER002   | John Doe

However I would like output like the below example, with the software name from the where statement as well as its version:但是,我希望输出如下例所示,其中包含 where 语句中的软件名称及其版本:

Computer Name | User Name | Software     | Version
--------------+-----------+--------------+---------
COMPUTER001   | Jane Doe  | Adobe Reader |  v.XXX
COMPUTER002   | John Doe  | Adobe Reader |  v.XXX

I have tried adding to my Select statement these properties:我曾尝试将这些属性添加到我的 Select 语句中:

SELECT DISTINCT
    sys.Netbios_Name0 AS [Computer Name],
    sys.User_Name0 AS [User Name],
    arp.DisplayName0 AS [Software],
    arp.Version0 AS [Version]

But this returns EVERY software a computer has, like so:但这会返回计算机拥有的每个软件,如下所示:

Computer Name | User Name | Software     | Version
--------------+-----------+--------------+--------
COMPUTER001   | Jane Doe  | Adobe Reader |  v.XXX
COMPUTER001   | Jane Doe  | Firefox      |  v.XXX
COMPUTER001   | Jane Doe  | Chrome       |  v.XXX

I need it just to give the specific software I query.我需要它只是为了提供我查询的特定软件。 I have searched on the internet to see if I could find anything to help me, and started learning some SQL basics, but haven't had much luck so far.我在互联网上搜索过是否能找到任何可以帮助我的东西,并开始学习一些 SQL 基础知识,但到目前为止还没有多少运气。 I am going to continue researching and working on it, but if anyone can offer me any tips or advice, I would really appreciate it!我将继续研究和工作,但如果有人能给我任何提示或建议,我将不胜感激!

By the way, I have managed to create the query successfully in SCCM, but I am not so sure how to translate WQL into SQL and work wants me to make the query in SSMS.顺便说一句,我已经设法在 SCCM 中成功创建了查询,但我不太确定如何将 WQL 转换为 SQL,并且工作需要我在 SSMS 中进行查询。

If I understand correctly, you don't really need an inner query (which is BTW bad practice and should be avoided at all costs, since this query runs on every row).如果我理解正确,您实际上并不需要内部查询(顺便说一句,这是不好的做法,应该不惜一切代价避免,因为此查询在每一行上运行)。 You query can be simplified to code below to achieve what you need.您可以将查询简化为下面的代码以实现您的需求。

SELECT DISTINCT
    sys.Netbios_Name0 AS [Computer Name],
    sys.User_Name0 AS [User Name],
    arp.DisplayName0 AS [Software],
    arp.Version0 AS [Version]
FROM v_R_System sys
JOIN v_Add_Remove_Programs arp ON sys.ResourceID = arp.ResourceID
WHERE arp.DisplayName0 LIKE '%Adobe Reader%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM