Skip to content

Script Change for instaWDB in Drop Database Section - #1311

Merged
Umachandar Jayachandran (uc-msft) merged 3 commits into
microsoft:masterfrom
Imran-imtiaz48:change
Aug 31, 2026
Merged

Script Change for instaWDB in Drop Database Section#1311
Umachandar Jayachandran (uc-msft) merged 3 commits into
microsoft:masterfrom
Imran-imtiaz48:change

Conversation

@Imran-imtiaz48

Copy link
Copy Markdown

Declare Variable: The script starts by declaring a variable dbname to hold the database name.
Check for Existence: It checks if the database exists.
Close Existing Connections: If the database exists, it closes all existing connections by setting the database to SINGLE_USER mode with immediate rollback of active transactions.
Drop the Database: It then drops the database.
Error Handling: After attempting to drop the database, it checks again if the database still exists. If it does, it raises an error indicating there are still open connections.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the revised code is more robust and user-friendly. It handles existing connections more gracefully, provides clear feedback messages, and uses dynamic SQL for better flexibility. These improvements make the script more reliable and easier to maintain.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the SQL Injection issues.

BEGIN
-- Close existing connections to the database
DECLARE @SQL NVARCHAR(MAX) = N'';
SELECT @SQL += 'ALTER DATABASE [' + @DBName + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modify to use QUOTENAME(dbname). Current string concatenation technique is susceptible to SQL injection attacks.

EXEC sp_executesql @SQL;

-- Drop the database
SET @SQL = N'DROP DATABASE [' + @DBName + '];';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Get the quoted identifier first & then concatenate.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL script effectively creates the authors, publishers, and titles tables with necessary constraints and defaults. However, there are areas for improvement to enhance readability and maintainability. Consider using a consistent naming convention for constraints and indexes. Simplify the CHECK constraints for au_id and zip using regex patterns. Ensure all default values are meaningful, especially for the phone and type columns. Additionally, aligning the data types and constraints formatting improves clarity. Here's an example of a refined version:

CREATE TABLE authors (
    au_id CHAR(11) CHECK (au_id LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]') CONSTRAINT PK_authors PRIMARY KEY,
    au_lname VARCHAR(40) NOT NULL,
    au_fname VARCHAR(20) NOT NULL,
    phone CHAR(12) NOT NULL DEFAULT 'UNKNOWN',
    address VARCHAR(40) NULL,
    city VARCHAR(20) NULL,
    state CHAR(2) NULL,
    zip CHAR(5) CHECK (zip LIKE '[0-9][0-9][0-9][0-9][0-9]'),
    contract BIT NOT NULL
);

CREATE TABLE publishers (
    pub_id CHAR(4) NOT NULL CONSTRAINT PK_publishers PRIMARY KEY,
    pub_name VARCHAR(40) NULL,
    city VARCHAR(20) NULL,
    state CHAR(2) NULL,
    country VARCHAR(30) NULL DEFAULT 'USA',
    CHECK (pub_id IN ('1389', '0736', '0877', '1622', '1756') OR pub_id LIKE '99[0-9][0-9]')
);

CREATE TABLE titles (
    title_id CHAR(6) CONSTRAINT PK_titles PRIMARY KEY,
    title VARCHAR(80) NOT NULL,
    type CHAR(12) NOT NULL DEFAULT 'UNDECIDED',
    pub_id CHAR(4) NULL REFERENCES publishers(pub_id),
    price MONEY NULL,
    advance MONEY NULL,
    royalty INT NULL,
    ytd_sales INT NULL,
    notes VARCHAR(200) NULL,
    pubdate DATETIME NOT NULL DEFAULT (GETDATE())
);

By implementing these changes, the script will become more consistent and easier to maintain.

@uc-msft
Umachandar Jayachandran (uc-msft) merged commit ee4cca5 into microsoft:master Aug 31, 2026
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants