SQL Server: How to check if a WORKLAD GROUP and a RESOURCE POOL exists - sql-server-2016

In my SQL script, I want to drop a "WORKLOAD GROUP" and a "RESOURCE POOL", but it might be that they do not exist. How can I check?
I'm using SQL Server 2016.
Thanks in advance!
Frank

I've got the solution from another forum:
IF EXISTS (SELECT * FROM sys.resource_governor_resource_pools WHERE name = N'MyPool')
DROP RESOURCE POOL MyPool
GO
IF EXISTS (SELECT * FROM sys.resource_governor_workload_groups WHERE name = N'MyGroup') DROP WORKLOAD GROUP MyGroup GO

Related

Get login name in SQL SERVER

I have an orphan user in sql server and I want to get the login name not the user name because they are not the same, I didn't find a solution that correspond to my needs.
You should be able to get it using :
SELECT suser_name()
Docs Link : https://learn.microsoft.com/en-us/sql/t-sql/functions/suser-name-transact-sql
I believe that this is what you need:
SELECT ORIGINAL_LOGIN()
execute as login = 'LCF\jmp'
select distinct name
from sys.login_token
where principal_id > 0
and type = 'WINDOWS GROUP';
Your user is Windows user, so it can login to server due to its membership in some Windows groups.
To find out these groups run the query:
execute as login = 'LCF\jmp'
select distinct name
from sys.login_token
where principal_id > 0
and type = 'WINDOWS GROUP';
And mind that it's not "orphaned", it need not to have it's sid mapped to server to be able to connect to server, Windows groups sids are enough.
On MSSQL 2014 use
select SYSTEM_USER

Get SQL Server database created Information

I have created a database in SQL Server 2012 with name "yadav". Now I want to know where "Yadav" DB info is stored like Created Date, Number of Tables, User Name etc.
Thanks in advance.
Some basic things are accessible in the sys.databases catalog view:
SELECT name, database_id, create_date
FROM sys.databases
WHERE name = 'yadav'
For other, you'll have to go to your database:
USE yadav;
SELECT TableCount = COUNT(*)
FROM sys.tables
and there's a vast collection of other system catalog views which provide insight into your database and its objects
You need something like this
sp_helpdb 'Yadav'
You can get the database information from sys.databases table.
select *
from sys.databases
where name = database_name
and table information from INFORMATION_SCHEMA.COLUMNS table.
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG = database_name

SQL Server Find Out When a Table Was Renamed?

Is it possible to find out when a table was renamed? (as a primary question)
And if possible which user did it?
You can try this:
SELECT Name, modify_date, * FROM sys.all_objects
WHERE Name = 'Table name'

How to list active connections on PostgreSQL?

Is there a command in PostgreSQL to select active connections to a given database?
psql states that I can't drop one of my databases because there are active connections to it, so I would like to see what the connections are (and from which machines)
Oh, I just found that command on PostgreSQL forum:
SELECT * FROM pg_stat_activity;
Following will give you active connections/ queries in postgres DB-
SELECT
pid
,datname
,usename
,application_name
,client_hostname
,client_port
,backend_start
,query_start
,query
,state
FROM pg_stat_activity
WHERE state = 'active';
You may use 'idle' instead of active to get already executed connections/queries.
SELECT * FROM pg_stat_activity WHERE datname = 'dbname' and state = 'active';
Since pg_stat_activity contains connection statistics of all databases having any state, either idle or active, database name and connection state should be included in the query to get the desired output.
You can check connection details in Postgres using pg_stat_activity. You can apply filter to satisfy your condition. Below are queries.
References: https://orahow.com/check-active-connections-in-postgresql/
SELECT * FROM pg_stat_activity WHERE state = 'active';
select * from pg_stat_activity where state = 'active' and datname = 'REPLACE_DB_NAME_HERE';
If you would like to use PgAdmin (for me it is more than convenient), you could do these simple steps. Glad if this is helps
SELECT * FROM pg_stat_activity;

How can I Retrieve a List of All Table Names from a SQL Server CE database?

Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';