Tuesday, July 5, 2022

T-SQL Query to get the data for inventory

Hi All,

The following query can be used to get the data needed for an Inventory. This includes the server name, version, patching info etc., We can create registered server and add all the servers available with us and run the query in centralized manner. in one shot we can gather all the required details needed for an inventory.

Code:

Set NoCount On
Declare @CurrID int,@ExistValue int, @MaxID int, @SQL nvarchar(1000)
Declare @TCPPorts Table (PortType nvarchar(180), Port int)
Declare @SQLInstances Table (InstanceID int identity(1, 1) not null primary key,
                                          InstName nvarchar(180),
                                          Folder nvarchar(50),
                                          StaticPort int null,
                                          DynamicPort int null,
                                          Platform int null);
Declare @Plat Table (Id int,Name varchar(180),InternalValue varchar(50), Charactervalue varchar (50))
Declare @Platform varchar(100)
Insert into @Plat exec xp_msver platform
select @Platform = (select 1 from @plat where charactervalue like '%86%')
If @Platform is NULL
Begin
Insert Into @SQLInstances (InstName, Folder)
Exec xp_regenumvalues N'HKEY_LOCAL_MACHINE',
                             N'SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL';
Update @SQLInstances set Platform=64
End
else
Begin
Insert Into @SQLInstances (InstName, Folder)
Exec xp_regenumvalues N'HKEY_LOCAL_MACHINE',
                             N'SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL';
Update @SQLInstances Set Platform=32
End 
Declare @Keyexist Table (Keyexist int)
Insert into @Keyexist
Exec xp_regread'HKEY_LOCAL_MACHINE',
                              N'SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL';
select @ExistValue= Keyexist from @Keyexist
If @ExistValue=1
Insert Into @SQLInstances (InstName, Folder)
Exec xp_regenumvalues N'HKEY_LOCAL_MACHINE',
                              N'SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL';
Update @SQLInstances Set Platform =32 where Platform is NULL
Select @MaxID = MAX(InstanceID), @CurrID = 1
From @SQLInstances
While @CurrID <= @MaxID
  Begin
      Delete From @TCPPorts
      Select @SQL = 'Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
                              N''SOFTWARE\Microsoft\\Microsoft SQL Server\' + Folder + '\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
                              N''TCPDynamicPorts'''
      From @SQLInstances
      Where InstanceID = @CurrID
      Insert Into @TCPPorts
      Exec sp_executesql @SQL
      Select @SQL = 'Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
                             N''SOFTWARE\Microsoft\\Microsoft SQL Server\' + Folder + '\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
                              N''TCPPort'''
      From @SQLInstances
      Where InstanceID = @CurrID
      Insert Into @TCPPorts
      Exec sp_executesql @SQL
      Select @SQL = 'Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',
                              N''SOFTWARE\Wow6432Node\Microsoft\\Microsoft SQL Server\' + Folder + '\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'',
                              N''TCPDynamicPorts'''
      From @SQLInstances
      Where InstanceID = @CurrID
      Insert Into @TCPPorts
      Exec sp_executesql @SQL
      Select @SQL = 'Exec xp_instance_regread N''HKEY_LOCAL_MACHINE'',                              N''SOFTWARE\Wow6432Node\Microsoft\\Microsoft SQL Server\' + Folder + '\MSSQLServer\SuperSocketNetLib\Tcp\IPAll'', N''TCPPort''' From @SQLInstances
Where InstanceID = @CurrID
      Insert Into @TCPPorts
      Exec sp_executesql @SQL
      Update SI
      Set StaticPort = P.Port,
            DynamicPort = DP.Port
      From @SQLInstances SI
      Inner Join @TCPPorts DP On DP.PortType = 'TCPDynamicPorts'
      Inner Join @TCPPorts P On P.PortType = 'TCPPort'
      Where InstanceID = @CurrID;
      Set @CurrID = @CurrID + 1
  End
Select serverproperty('ComputerNamePhysicalNetBIOS') as ServerName, InstName,(select COUNT(*) from sys.dm_os_schedulers where status = 'VISIBLE ONLINE') as logical_processors , (select (cpu_count / hyperthread_ratio) 'physical_cpus' from sys.dm_os_sys_info) as phiysical_processors, (select cast(round(cast(physical_memory_kb AS Numeric(15,4))/1024,2) as decimal(10,2)) from sys.dm_os_sys_info )as totalsize,StaticPort, DynamicPort,Platform,case serverproperty('IsClustered')
when 0 then 'No'
when 1 then 'Yes'
end as IsClust,
case serverproperty('Ishadrenabled')
when 0 then 'Disabled'
when 1 then 'Enabled'
else 'Not Applicable'
end as AlwaysON,
SERVERPROPERTY('productversion') as SQLVersion, SERVERPROPERTY('ProductLevel') as ServicePacklevel
From @SQLInstances
Set NoCount Off


Thanks for the Anonymous commenter, I have added his/her suggestion in the script.

How to Fix SQL Server Data Tools (SSDT) ​​Installation Error 2017 – Setup Failed: Incorrect function (0x80070001)

In this post, I would like to share how to resolve an error installing SQL Server Data Tools (SSDT) ​​2017 - Setup Failed: Incorrect function (0x80070001), which happens to me frequently whenever I try to install SQL Server Data Tools 2017 version on some computers.



What is SQL Server Data Tools (SSDT)?

According to the official Microsoft documentation, SQL Server Data Tools (SSDT) ​​transforms database development by introducing a ubiquitous declarative model that spans all phases of database development in Visual Studio.

You can use SSDT's Transact-SQL design capabilities to compile, debug, maintain, and refactor databases. It can work with a database project or directly with a connected or external database instance.

In summary, SSDT is a free IDE for BI and database analysts to work with database projects (SQL Database Projects / SQLCLR), Integration Service projects (SSIS), Analysis Services (SSAS) and Reporting Services (SSRS) , so it is essential for anyone working in the data field with Microsoft data tools.

If we analyze the SSDT download page, we can see that versions 2019 and 2022 (preview) already exist for download, as components of Visual Studio, while versions 2017 and earlier were standalone installers that already installed a simplified copy of Visual Studio, known as Visual Studio Shell ( SSDT 2014 to 2017) or BIDS (SSDT 2005 to 2012).

The biggest problem I see with newer versions and ends up generating many doubts throughout the technical community, is that you need to install Visual Studio Community Edition to use SSDT, if you don't have a Visual Studio license.

If we analyze the Official Visual Studio Community Documentation, note that using the Community edition has some limitations for commercial use:

Translation: For all other usage scenarios: In non-enterprise organizations, up to five users can use Visual Studio Community. In business organizations (ie, those with over 250 PCs or over $1 million in annual revenue), no use is allowed beyond the open source, academic research, and classroom learning environment scenarios described above .

That is, if you work for a large company, you cannot use Visual Studio Community. And what about SSDT? How would you be in this situation? Well, so far, I haven't found any official documentation mentioning this exception, so many people are afraid to use the newer versions of SSDT because of this licensing question and end up opting for the 2017 version, the latest version with standalone installer.

Error installing SQL Server Data Tools (SSDT) ​​2017

Going back to the original problem of this article, sometimes I can install SSDT 2017 normally and sometimes I can't because the error Incorrect function (0x80070001) happens during the installation.

Analyzing the installation error log, I couldn't identify a possible cause or solution for this.

To resolve this, we will do offline installation of SSDT 2017 as the issue occurs when trying to download some features from the installer. Download standalone installer this link here ou click on this other link here to go straight to the download link.

Download the vs_sql.exe file, clicking on this link here and save in the same directory where you downloaded the SSDT installer (SSDT-Setup-ENU.exe).

Open the DOS prompt as an administrator:


Now navigate to the folder where you downloaded the files and type the command:

After that, the installation will start and the files will be downloaded to the chosen directory:


During the download process, I noticed it was over 1h at 69.62%. I pressed the “Enter” key, the installer showed this error message and continued the process.

Installation completed with errors:

To fix this problem that occurred to me, type the command below: (only do this if you also had download errors)

Navigate to the directory where you downloaded the layout files (in my case, C:\SSDT2017) using the “cd” command and type the command below in the Command Prompt window to install Visual Studio Shell and Database Project:

There's not much to change here, just click on the Install button (or maybe change the installation path):

Visual Studio Data Tools 2017 Installed

Now you can install SQL Server Data Tools 2017 (SSDT) ​​through the normal installer (SSDT-Setup-ENU.exe), remembering to check the SSIS, SSRS and SSAS options:

At the end of the process, we see that the installation was completed successfully!

After restarting the server, I open Visual Studio 2017:

And the SQL Server project templates (Database Project / SQLCLR), Analysis Services (SSAS), Integration Services (SSIS) and Reporting Services (SSRS) are working normally.