Wednesday, November 6, 2024

I have multiple databases with the same table structure, and each database name starts with 'U'. I want to find the row count of the Traces table in each of these databases. Could you provide a SQL script that loops through each database starting with 'U', checks if the Traces table exists, and retrieves the row count for it in each database?

 USE master;

DECLARE @DatabaseName NVARCHAR(128);

DECLARE @sql NVARCHAR(MAX);


-- Cursor to iterate over each database starting with "U"

DECLARE db_cursor CURSOR FOR

SELECT name 

FROM sys.databases

WHERE name LIKE 'U%'  -- Filters databases starting with "U"

  AND state_desc = 'ONLINE'; -- Only includes online databases


-- Open the cursor

OPEN db_cursor;

FETCH NEXT FROM db_cursor INTO @DatabaseName;


-- Loop through each database

WHILE @@FETCH_STATUS = 0

BEGIN

    -- Construct the dynamic SQL for each database

    SET @sql = 'USE ' + QUOTENAME(@DatabaseName) + ';

                IF EXISTS (SELECT 1 FROM ' + QUOTENAME(@DatabaseName) + '.sys.tables WHERE name = ''Traces'')

                BEGIN

                    SELECT ''' + @DatabaseName + ''' AS DatabaseName, 

                           COUNT(*) as rowscount 

                    FROM ' + QUOTENAME(@DatabaseName) + '.dbo.Traces;

                END';

    

    -- Execute the dynamic SQL

    EXEC sp_executesql @sql;


    -- Move to the next database

    FETCH NEXT FROM db_cursor INTO @DatabaseName;

END


-- Close and deallocate the cursor

CLOSE db_cursor;

DEALLOCATE db_cursor;


Tuesday, November 5, 2024

Querystring Issue on C# with special characters

 If you're building a URL string dynamically in C#, you can use Uri.EscapeDataString to ensure proper encoding:

string searchTerm = "rock&roll";

string query = $"https://example.com/search?query={Uri.EscapeDataString(searchTerm)}";


This approach will automatically encode any special characters, including &, resulting in a safe and valid query string.


Tuesday, October 22, 2024

SQL Script to Terminate All Active Connections to a Specific Database || Kill Database connections

 -- Declare a cursor to iterate through the sessions

DECLARE @sessionId INT;

DECLARE kill_cursor CURSOR FOR

    SELECT session_id

    FROM sys.dm_exec_sessions

    WHERE database_id = DB_ID('DBName');


-- Open the cursor and loop through the sessions

OPEN kill_cursor;


FETCH NEXT FROM kill_cursor INTO @sessionId;


WHILE @@FETCH_STATUS = 0

BEGIN

    -- Print the session ID to be killed (for verification purposes)

    PRINT 'Killing session: ' + CAST(@sessionId AS VARCHAR(10));


    -- Kill the session

    EXEC('KILL ' + @sessionId);


    FETCH NEXT FROM kill_cursor INTO @sessionId;

END


-- Close and deallocate the cursor

CLOSE kill_cursor;

DEALLOCATE kill_cursor;

Monday, September 9, 2024

How do I create a unique constraint on a column in SQL Server?

 You can create a unique constraint using the ALTER TABLE command:

ALTER TABLE YourTableName ADD CONSTRAINT Unique_Constraint_Name UNIQUE (ColumnName);

This enforces a unique constraint on ColumnName.

This returns the names of all databases on the server.

 To remove a column from a table, use the ALTER TABLE command:

ALTER TABLE YourTableName DROP COLUMN ColumnName;

This will permanently delete the column from the table.

How do I list all available databases on a SQL Server instance?

 You can list all databases on a SQL Server instance using:

SELECT name FROM sys.databases;

This returns the names of all databases on the server.

How do I check the SQL Server version and edition?

 Use the following query to get the SQL Server version and edition:

SELECT SERVERPROPERTY('ProductVersion') AS Version, 

 SERVERPROPERTY('ProductLevel') AS ProductLevel,

 SERVERPROPERTY('Edition') AS Edition;

This returns the version, product level (e.g., RTM, SP1), and edition of the SQL Server instance.

How do I rename a column in SQL Server?

 You can rename a column using the sp_rename system stored procedure:

EXEC sp_rename 'YourTableName.OldColumnName', 'NewColumnName', 'COLUMN';

This renames OldColumnName to NewColumnName in the specified table.

How do I grant a user access to a specific database in SQL Server?

 To grant access to a user for a specific database, use the following commands:

Create a login (if it doesn't already exist):

CREATE LOGIN YourLoginName WITH PASSWORD = 'YourPassword';

Grant access to the database:

USE YourDatabaseName; CREATE USER YourLoginName FOR LOGIN YourLoginName;

Grant a role or specific permissions:

EXEC sp_addrolemember 'db_datareader', 'YourLoginName';  -- Grants read access



How do I execute a stored procedure in SQL Server?

 To execute a stored procedure, use the EXEC command followed by the procedure name and any necessary parameters:

EXEC YourStoredProcedureName @Param1 = 'Value1', @Param2 = 'Value2';

Replace YourStoredProcedureName with the name of the stored procedure and provide the required parameter values.