Transact SQL scripts commonly used in SQL Server database

Time:2020-2-15

 

data base

1. Create database

USE master ;  
GO  
CREATE DATABASE Sales  
ON   
( NAME = Sales_dat,  
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\saledat.mdf',  
    SIZE = 10,  
    MAXSIZE = 50,  
    FILEGROWTH = 5 )  
LOG ON  
( NAME = Sales_log,  
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\salelog.ldf',  
    SIZE = 5MB,  
    MAXSIZE = 25MB,  
    FILEGROWTH = 5MB ) ;  
GO 

 

2. View database

SELECT name, database_id, create_date  
FROM sys.databases ;

 

3. Delete database

DROP DATABASE Sales;

 

surface

1. Create table

CREATE TABLE PurchaseOrderDetail  
(  
    ID uniqueidentifier NOT NULL  
    ,LineNumber smallint NOT NULL  
    ,ProductID int NULL  
    ,UnitPrice money NULL  
    ,OrderQty smallint NULL  
    ,ReceivedQty float NULL  
    ,RejectedQty float NULL  
    ,DueDate datetime NULL  
);  

 

2. Delete table

DROP TABLE dbo.PurchaseOrderDetail;  

 

3. List of names

EXEC sp_rename 'Sales.SalesTerritory', 'SalesTerr';  

 

column

1. Add column

ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ;

 

2. Delete column

ALTER TABLE dbo.doc_exb DROP COLUMN column_b;

 

3. Name list

EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';

 

constraint

1. Primary key


ALTER TABLE Production.TransactionHistoryArchive
   ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);


CREATE TABLE Production.TransactionHistoryArchive1
   (
      TransactionID int IDENTITY (1,1) NOT NULL
      , CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID)
   )
;



SELECT name  
FROM sys.key_constraints  
WHERE type = 'PK' AND OBJECT_NAME(parent_object_id) = N'TransactionHistoryArchive';  
GO  


ALTER TABLE Production.TransactionHistoryArchive  
DROP CONSTRAINT PK_TransactionHistoryArchive_TransactionID;   
GO  

 

view

1. Create view

CREATE VIEW V_EmployeeHireDate  
AS  
SELECT p.FirstName, p.LastName, e.HireDate  
FROM HumanResources.Employee AS e JOIN Person.Person AS  p  
ON e.BusinessEntityID = p.BusinessEntityID ;   
GO  

 

2. Delete view

DROP VIEW V_EmployeeHireDate;  

 

stored procedure

1. Create stored procedure

CREATE PROCEDURE P_UspGetEmployeesTest   
    @LastName nvarchar(50),   
    @FirstName nvarchar(50)   
AS   
    SELECT FirstName, LastName, Department  
    FROM HumanResources.vEmployeeDepartmentHistory  
    WHERE FirstName = @FirstName AND LastName = @LastName  
    AND EndDate IS NULL;  
GO  

 

2. Delete stored procedure

DROP PROCEDURE P_UspGetEmployeesTest;

 

3. Execute stored procedure

EXEC P_UspGetEmployeesTest N'Ackerman', N'Pilar';  

EXEC P_UspGetEmployeesTest @LastName = N'Ackerman', @FirstName = N'Pilar';  
GO  

EXECUTE P_UspGetEmployeesTest @FirstName = N'Pilar', @LastName = N'Ackerman';  
GO  

 

4. Rename stored procedure

EXEC sp_rename 'P_UspGetAllEmployeesTest', 'P_UspEveryEmployeeTest2'; 

 

5. Stored procedure with output parameters

CREATE PROCEDURE P_UspGetEmployeeSalesYTD  
@SalesPerson nvarchar(50),  
@SalesYTD money OUTPUT  
AS    
    SELECT @SalesYTD = SalesYTD  
    FROM SalesPerson AS sp  
    JOIN vEmployee AS e ON e.BusinessEntityID = sp.BusinessEntityID  
    WHERE LastName = @SalesPerson;  
RETURN  
GO



DECLARE @SalesYTDBySalesPerson money;  
EXECUTE P_UspGetEmployeeSalesYTD  
    N'Blythe', 
    @SalesYTD = @SalesYTDBySalesPerson OUTPUT; 
GO  

 

cursor


DECLARE @Id uniqueidentifier
DECLARE @Name nvarchar(50)
DECLARE @Gender bit
DECLARE @CreateTime nvarchar(50)


DECLARE Test_Cursor CURSOR FOR SELECT [Id],[Name],[Gender],[CreateTime] FROM Test


OPEN Test_Cursor


FETCH NEXT FROM Test_Cursor INTO @Id,@Name,@Gender,@CreateTime
WHILE @@FETCH_STATUS = 0
BEGIN
    
    
    UPDATE Test SET [Name] = [Name] + 'abc' WHERE CURRENT OF Test_Cursor

    IF(@Id = '4D2F97EF-8FEB-4183-BFD0-089222000F69')
    BEGIN
        
        DELETE FROM Test WHERE CURRENT OF Test_Cursor
    END

    
    FETCH NEXT FROM Test_Cursor INTO @Id,@Name,@Gender,@CreateTime
END;

CLOSE Test_Cursor;

DEALLOCATE Test_Cursor;  

 

 

data type