How can I get the SQL Server server and instance name of the current connection, using a T-SQL script?
Just found the answer, in this SO question (literally, inside the question, not any answer):
SELECT ##servername
returns servername\instance as far as this is not the default instance
SELECT ##servicename
returns instance name, even if this is the default (MSSQLSERVER)
How about this:
EXECUTE xp_regread #rootkey='HKEY_LOCAL_MACHINE',
#key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl',
#value_name='MSSQLSERVER'
This will get the instance name as well. null means default instance:
SELECT SERVERPROPERTY ('InstanceName')
http://technet.microsoft.com/en-us/library/ms174396.aspx
SELECT ##servername will give you data as server/instanceName
To get only the instanceName you should run select ##ServiceName query .
Why stop at just the instance name? You can inventory your SQL Server environment with following:
SELECT
SERVERPROPERTY('ServerName') AS ServerName,
SERVERPROPERTY('MachineName') AS MachineName,
CASE
WHEN SERVERPROPERTY('InstanceName') IS NULL THEN ''
ELSE SERVERPROPERTY('InstanceName')
END AS InstanceName,
'' as Port, --need to update to strip from Servername. Note: Assumes Registered Server is named with Port
SUBSTRING ( (SELECT ##VERSION),1, CHARINDEX('-',(SELECT ##VERSION))-1 ) as ProductName,
SERVERPROPERTY('ProductVersion') AS ProductVersion,
SERVERPROPERTY('ProductLevel') AS ProductLevel,
SERVERPROPERTY('ProductMajorVersion') AS ProductMajorVersion,
SERVERPROPERTY('ProductMinorVersion') AS ProductMinorVersion,
SERVERPROPERTY('ProductBuild') AS ProductBuild,
SERVERPROPERTY('Edition') AS Edition,
CASE SERVERPROPERTY('EngineEdition')
WHEN 1 THEN 'PERSONAL'
WHEN 2 THEN 'STANDARD'
WHEN 3 THEN 'ENTERPRISE'
WHEN 4 THEN 'EXPRESS'
WHEN 5 THEN 'SQL DATABASE'
WHEN 6 THEN 'SQL DATAWAREHOUSE'
END AS EngineEdition,
CASE SERVERPROPERTY('IsHadrEnabled')
WHEN 0 THEN 'The Always On Availability Groups feature is disabled'
WHEN 1 THEN 'The Always On Availability Groups feature is enabled'
ELSE 'Not applicable'
END AS HadrEnabled,
CASE SERVERPROPERTY('HadrManagerStatus')
WHEN 0 THEN 'Not started, pending communication'
WHEN 1 THEN 'Started and running'
WHEN 2 THEN 'Not started and failed'
ELSE 'Not applicable'
END AS HadrManagerStatus,
CASE SERVERPROPERTY('IsSingleUser') WHEN 0 THEN 'No' ELSE 'Yes' END AS InSingleUserMode,
CASE SERVERPROPERTY('IsClustered')
WHEN 1 THEN 'Clustered'
WHEN 0 THEN 'Not Clustered'
ELSE 'Not applicable'
END AS IsClustered,
'' as ServerEnvironment,
'' as ServerStatus,
'' as Comments
I found this:
EXECUTE xp_regread
#rootkey = 'HKEY_LOCAL_MACHINE',
#key = 'SOFTWARE\Microsoft\Microsoft SQL Server',
#value_name = 'InstalledInstances'
That will give you list of all instances installed in your server.
The ServerName property of the SERVERPROPERTY function and ##SERVERNAME return similar information. The ServerName property provides the Windows server and instance name that together make up the unique server instance. ##SERVERNAME provides the currently configured local server name.
And Microsoft example for current server is:
SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
This scenario is useful when there are multiple instances of SQL Server installed on a Windows server, and the client must open another connection to the same instance used by the current connection.
To get the list of server and instance that you're connected to:
select * from Sys.Servers
To get the list of databases that connected server has:
SELECT * from sys.databases;
Just to add some clarification to the registry queries. They only list the instances of the matching bitness (32 or 64) for the current instance.
The actual registry key for 32-bit SQL instances on a 64-bit OS is:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server
You can query this on a 64-bit instance to get all 32-bit instances as well. The 32-bit instance seems restricted to the Wow6432Node so cannot read the 64-bit registry tree.
You can get your server name, machine name and instance name with Transact-SQL(T-SQL) as shown below:
SELECT ##SERVERNAME -- DESKTOP-OVPADTC\SQLEXPRESS
SELECT SERVERPROPERTY ('ServerName') -- DESKTOP-OVPADTC\SQLEXPRESS
SELECT HOST_NAME() -- DESKTOP-OVPADTC
SELECT SERVERPROPERTY ('MachineName') -- DESKTOP-OVPADTC
SELECT ##SERVICENAME -- SQLEXPRESS
SELECT SERVERPROPERTY ('InstanceName') -- SQLEXPRESS
another method to find Instance name- Right clck on Database name and select Properties, in this part you can see view connection properties in left down corner, click that then you can see the Instance name.
Related
The SQL Server print messages are not taken into chef inspec for validation. Do we have an option to validate.
Below chef inspec sample code donot take the SQL Server print message.
sql = mssql_session(user: 'sa', password: 'Test')
describe sql.query("
-- #5.2 - Set the ''default trace enabled'' Server Configuration Option to 1
IF EXISTS (SELECT name, CAST(value as int) as value_configured, CAST(value_in_use as int) as value_in_use FROM sys.configurations WHERE name = 'default trace enabled' and (value=1 and value_in_use=1))
BEGIN
PRINT 'OK'
END
ELSE
PRINT 'NOT OK'") do
its("value") { should eq 'OK' }
end
end
Instead of PRINT 'OK' use SELECT 'OK' AS value / SELECT 'NOT OK' AS value.
Programmatically getting a hold of PRINT messages can be done but you need code to listen to the connection's InfoMessage event. Basically you'd need to dig into the code for either mssql_session or sql.query... which you probably don't want to do.
You can modify your statement to directly return OK or NOT OK, as given below. SELECT returns the resultset back. Print does not return result set back. The drivers have to read it differently using InfoMessage event. Read more.
Better to go for SELECT statement, as suggested by #user2845090.
SELECT CASE WHEN
EXISTS (SELECT name
, CAST(value as int) as value_configured
, CAST(value_in_use as int) as value_in_use
FROM sys.configurations WHERE name = 'default trace enabled'
and (value=1 and value_in_use=1)) THEN 'OK' ELSE 'NOT OK'
END
Or
SELECT IIF(EXISTS((SELECT name
, CAST(value as int) as value_configured
, CAST(value_in_use as int) as value_in_use
FROM sys.configurations WHERE name = 'default trace enabled'
and (value=1 and value_in_use=1)),'OK','NOT OK')
Consider I have text from the field (notes) as below:
Please check http://example.com
I want a SQL query which fetches the link part only. That is to find keyword http and print till the last.
Output:
http://example.com
Also if the text field doesnt have any link, can we print NA?
CASE WHEN sys like '%Clo%'
THEN RIGHT( i.notes,LEN(i.notes) - CHARINDEX('http',i.notes,1)+1)
ELSE "No Link Available" END AS Cl_Link
For SQL Server you can consider this below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
For MySQL-
SET #T = 'Please check http://example.com';
SELECT RIGHT(#T,LENGTH(#T) - POSITION("http:" IN #T)+1)
In case of select query using table, queries will be-
-- SQL Server
SELECT RIGHT(column_name,LEN(column_name) - CHARINDEX('http:',column_name,1)+1) FROM your_table_name
-- MySQL
SELECT RIGHT(column_name,LENGTH(column_name) - POSITION("http:" IN column_name)+1) FROM your_table_name
To apply 'NA' when no link available, please use the below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT
CASE
WHEN CHARINDEX('http:',#T,1) >= 1 THEN RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
ELSE 'NA'
END
Below is the query for your reference, executed on mysql:
SELECT substr(columnname,locate('http',columnname)) as link
FROM `tablename` where column=value
For MySQL try this:
select REGEXP_SUBSTR(text_column, 'http\:\/\/([\\w\\.\\S]+)') from mytest
I found some strange information in log for login auditing. I am using Windows authentication and there are 2 different entries for same user. Sometimes there is ComputerName\User and sometimes MicrosoftAccount\user#email.com. This two accounts are connected. How can i know which account will be used and when?
How can i extract MicrosoftAccount\user#email.com from SQL Server logins?
I am using this query but i am only getting ComputerName\User account with it
SELECT
CASE
WHEN
CHARINDEX('\', ##SERVERNAME) > 0
THEN
REPLACE(name, SUBSTRING(##SERVERNAME, 1, CHARINDEX('\', ##SERVERNAME) - 1), CAST(SERVERPROPERTY('MachineName') AS nvarchar(128)))
ELSE
REPLACE(name, ##SERVERNAME, CAST(SERVERPROPERTY('MachineName') AS nvarchar(128)))
END
AS name FROM sys.syslogins ORDER BY name
How to check whether we have installed sql server full version or client version ?
You can use Select ##version
Or to get more data:
SELECT SERVERPROPERTY('productversion')AS Product_version,
SERVERPROPERTY('productlevel')AS Product_level,
SERVERPROPERTY('edition')AS Edition
To get some more details like Edition, Service Pack.
select SERVERPROPERTY('MachineName')as 'Host Name',
SERVERPROPERTY('ServerName') as 'Instance Name' ,
SERVERPROPERTY('IsClustered') as 'Cluster' ,
SERVERPROPERTY('Edition') as 'Edition',
SERVERPROPERTY('ProductVersion') as 'version',
SERVERPROPERTY('Productlevel') as 'Service Pack',
SERVERPROPERTY('LicenseType') as 'LicenseType' ,
SERVERPROPERTY('NumLicenses') as 'NumLicenses'
I've created this query which it gonna return data like this:
SQL Server 2016 Standard Edition (64-bit) 13.0.5237.0 SP2
DECLARE
#productver VARCHAR(50) = (SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR(50)))
DECLARE
#version VARCHAR(30)= CAST(LEFT(#productver, CHARINDEX('.', #productver)-1) AS INT)
SELECT
'SQL Server '+CASE #version
WHEN 9
THEN '2005'
WHEN 10
THEN '2008'
WHEN 11
THEN '2012'
WHEN 12
THEN '2014'
WHEN 13
THEN '2016'
WHEN 14
THEN '2017'
ELSE 'Unknow Version'
END + ' ' + CAST(SERVERPROPERTY('edition') AS VARCHAR(50)) AS SQLServerEdition,
#productver AS ProductVersion,
SERVERPROPERTY('productlevel') AS ServicePack
In linux Terminal:
$ sqlcmd -S localhost -U sa
Note: Here sa is the username, change it if your username is different.
Provide your password then enter your sql server.
Write:
1> select ##VERSION
2> GO
Try this to know your SQL Server Version:
SELECT ##VERSION
I'm having some database troubles, and can't seem to find a solution anywhere for this specific problem. I'm trying to grab information from a database using a table on the database and comparing it to a table on a linked server
[3/7/14 10:10:14:181 EST] 00000021 SystemErr R org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: The operation could not be performed because OLE DB provider "SQLNCLI" for linked server "EADWH02" was unable to begin a distributed transaction.
### The error may involve com.moog.app.weldlog.dao.mybatis.WeldLogMapping.listEmployee-Inline
### The error occurred while setting parameters
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: The operation could not be performed because OLE DB provider "SQLNCLI" for linked server "EADWH02" was unable to begin a distributed transaction.
Here is one of the specific stored procedures that is throwing this error:
#workOrder varchar(25) = null,
#idPartNumber varchar(25) = null
begin
IF #workOrder is null
SELECT DISTINCT TOP 25 WO.WO_NBR, PARTS.PART_NBR
FROM EADWH02.MEDW.LBR.WO WO
JOIN EADWH02.MEDW.MFG.PARTS PARTS
ON PARTS.PART_KEY = WO.PART_KEY
AND PARTS.SRC_DB_KEY = WO.SRC_DB_KEY
WHERE (WO.WO_TYPE_KEY = '2' or WO.WO_TYPE_KEY = '3' or WO.WO_TYPE_KEY = '4' or WO.WO_TYPE_KEY = '5') and
(PARTS.PART_NBR like '' + #idPartNumber + '%')
ELSE IF #idPartNumber is null
SELECT DISTINCT TOP 25 WO.WO_NBR, PARTS.PART_NBR, dbo.getDescriptions(WO.WO_NBR) as strDescriptions
FROM EADWH02.MEDW.LBR.WO WO
JOIN EADWH02.MEDW.MFG.PARTS PARTS
ON PARTS.PART_KEY = WO.PART_KEY
AND PARTS.SRC_DB_KEY = WO.SRC_DB_KEY
WHERE ((WO.WO_TYPE_KEY = '2' or WO.WO_TYPE_KEY = '3' or WO.WO_TYPE_KEY = '4' or WO.WO_TYPE_KEY = '5') and
(WO.WO_NBR like '' + #workOrder))
ORDER BY WO.WO_NBR
end
((dbo.getDescriptions is a scalar-valued function and works fine))
And here is my xml mapping file in the project:
<select id="listWorkOrder" statementType="CALLABLE" parameterType="WorkOrder" resultMap="WorkOrderMap"> {
call listWorkOrder( #{workOrder, jdbcType=VARCHAR},
#{partNumber, jdbcType=VARCHAR})
}
</select>
Any chance anyone knows what's causing this error? This sql stored procedure breaks both on a remote server we're using and on a local server. Thanks for the assistance!