Sql Server数据库常用Transact-SQL脚本

Posted 区欠阝日

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql Server数据库常用Transact-SQL脚本相关的知识,希望对你有一定的参考价值。

 

数据库

1、创建数据库

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、查看数据库

SELECT name, database_id, create_date  
FROM sys.databases ;  

 

3、删除数据库

DROP DATABASE Sales;

 

1、创建表

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、删除表

DROP TABLE dbo.PurchaseOrderDetail;  

 

3、重命名表

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

 

1、添加列

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

 

2、删除列

ALTER TABLE dbo.doc_exb DROP COLUMN column_b;

 

3、重命名列

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

 

约束

1、主键

--在现有表中创建主键
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  

 

视图

 1、创建视图

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、删除视图

DROP VIEW V_EmployeeHireDate;  

 

存储过程

1、创建存储过程

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、删除存储过程

DROP PROCEDURE P_UspGetEmployeesTest; 

 

3、执行存储过程

EXEC P_UspGetEmployeesTest N\'Ackerman\', N\'Pilar\';  
-- Or  
EXEC P_UspGetEmployeesTest @LastName = N\'Ackerman\', @FirstName = N\'Pilar\';  
GO  
-- Or  
EXECUTE P_UspGetEmployeesTest @FirstName = N\'Pilar\', @LastName = N\'Ackerman\';  
GO  

 

4、重命名存储过程

EXEC sp_rename \'P_UspGetAllEmployeesTest\', \'P_UspEveryEmployeeTest2\'; 

 

5、带有输出参数的存储过程

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  

 

游标

--定义要使用的变量
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;  

 

 

数据类型

 

以上是关于Sql Server数据库常用Transact-SQL脚本的主要内容,如果未能解决你的问题,请参考以下文章

sql server 数据定义常用sql

Sql Server 常用函数

常用的sql server规范

SQL Server常用命令总结

SQL Server常用数据类型

SQL-SERVER 常用数据类型