码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server常见操作

时间:2016-05-27 20:03:26      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

1. 常见操作(增删改查)

技术分享
--1. Create Table
USE [MVC_000]
CREATE TABLE T_TableName
(  
    ID INT IDENTITY(1,1) PRIMARY KEY,
    Name VARCHAR(50) UNIQUE,  
    Age INT,
    HomeAddress VARCHAR(50)
)  

--2. Insert data into table:T_TableName
INSERT INTO T_TableName(Name,Age,HomeAddress) 
VALUES(Xiao Lin,29,SD.Road New York)

--3. Truncate table
IF OBJECT_ID(NT_TableName,NU) IS NOT NULL
BEGIN
    TRUNCATE TABLE T_TableName
END

--4. Delete record
IF EXISTS(SELECT * FROM T_TableName WHERE ID=1)
BEGIN
    DELETE T_TableName WHERE ID=1
END

--5. Drop table
IF OBJECT_ID(NT_TableName,NU) IS NOT NULL
BEGIN
    DROP TABLE T_TableName
END
View Code

2. 游标操作

技术分享
USE [DB_NAME]

DECLARE CSR_NAME CURSOR FOR
SELECT Column1,Column2 FROM [dbo].[XXX]
    
DECLARE @Column1 INT
DECLARE @Column2 INT

OPEN CSR_NAME
FETCH NEXT FROM CSR_NAME INTO @Column1, @Column2
WHILE @@FETCH_STATUS = 0
BEGIN
        
    IF @Column1>3
    BEGIN
        PRINT(First column is bigger than 3!)
    END
    ELSE IF @Column1=3
    BEGIN
        PRINT(First column is equals 3!)
    END
    ELSE
    BEGIN
        PRINT(First column is less than 3!)
    END
    FETCH NEXT FROM CSR_NAME INTO @Column1, @Column2
END
CLOSE CSR_NAME
DEALLOCATE CSR_NAME
View Code

3. 存储过程模板

技术分享
USE [DB_NAME]
CREATE PROC [dbo].[SP_NAME]
--Declare
@XXX1 INT,
@XXX2 INT
AS

SELECT * FROM dbo.XXX WHERE XXX1=@XXX1 AND XXX2=@XXX2
GO
View Code

4. 函数模板

 表值函数:

技术分享
--Create
USE [DB_NAME]
CREATE FUNCTION BZ_FUNCTION(@Type VARCHAR(1))
RETURNS @AllInfor TABLE (ID INT,NAME VARCHAR(50))
AS

BEGIN
    IF(@Type=A)
    BEGIN
        INSERT @AllInfor VALUES (1,LI LEI)
    END
    ELSE
    BEGIN
        INSERT @AllInfor VALUES (2,HAN MEIMEI)
    END
    RETURN
END

--Usage
SELECT * FROM BZ_FUNCTION(A)
View Code

内嵌表值函数(实质也是表值函数):

技术分享
CREATE FUNCTION IF_GetStudentList(@ClassName VARCHAR(10))
RETURNS TABLE
AS
RETURN (SELECT * FROM Students WHERE ClassName LIKE %+@ClassName+%)
--For Testing:SELECT * FROM IF_GetStudentList(1)
View Code

标量值函数:

技术分享
--Create
USE [DB_NAME]
CREATE FUNCTION BLZ_FUNCTION(@Type VARCHAR(50))
RETURNS VARCHAR(20)
AS

BEGIN
    DECLARE @Result VARCHAR(20)
    IF(LEN(@Type)>=8)
    BEGIN
        SET @Result=SUCCESSFULL!
    END
    ELSE
    BEGIN
        SET @Result=Failed!
    END
    RETURN @Result
END

--Usage
SELECT dbo.BLZ_FUNCTION(XXX.ColumnName) FROM [dbo].[XXX]
View Code

为什么只有内嵌表值函数而没有内嵌标量值函数:
表值函数返回一个表的数据,内嵌表值函数可以通过传入不同的参数获取不同的返回值列表;
而标量值返回的是一个值,已经无法对返回结果进行拆分,所以内嵌标量值函数没有任何意义;

5. 触发器模板

 Instead Of:

 在对数据做DML操作之前,需要对数据进行验证,如果验证失败,则该操作会被取消(这应该就是Instead of的具体体现了吧)。

技术分享
--Instead of means that the actual statement will be replaced by the trigger,so the actual statement will not be executed.
--[Instead trigger] will be executed after the execution of actual statement.
IF (OBJECT_ID(TRG_STU_INSTEADOF, TR) IS NOT NULL)
BEGIN
    DROP TRIGGER TRG_STU_INSTEADOF
END
GO

CREATE TRIGGER TRG_STU_INSTEADOF
ON [dbo].[Students]
    INSTEAD OF DELETE 
AS
    DECLARE @ID INT,@StuName VARCHAR(20)
    SELECT @ID=StudentID,@StuName=StudentName FROM deleted
    PRINT The delete of students information[ID:+CONVERT(VARCHAR,@ID)+;Name:+@StuName+] is successful!;
GO
View Code

 After:

技术分享
--By default,the type of trigger is after.(DML)
--[After trigger] will be executed after the execution of actual statement.
IF (OBJECT_ID(TRG_STU, TR) IS NOT NULL)
BEGIN
    DROP TRIGGER TRG_STU
END
GO
CREATE TRIGGER TRG_STU
ON [dbo].[Students]
    FOR INSERT 
AS
    DECLARE @ID INT,@StuName VARCHAR(20)
    SELECT @ID=StudentID,@StuName=StudentName FROM inserted
    PRINT The adding for students information[ID:+CONVERT(VARCHAR,@ID)+;Name:+@StuName+] is successful!;
GO
View Code

 6. 系统表操作

系统表SYSOBJECTS中的列XTYPE所代表的类型:

技术分享
--C:Check约束
--D:Default约束
--PK:PRIMARY KEY约束
--F:FOREIGN KEY约束
--UQ:UNIQUE约束

--L:LOG

--S:SYSTEM TABLES
--U:User TABLES
--V:View
--P:Procedure
--TF:表值函数
  --IF:内嵌表值函数(在一个函数内部嵌入一个表值函数,也属于表值函数)
--FN:标量值函数
--TR:Trigger

--X:Extension Procedure
--RF:复制筛选存储过程
View Code

常见用法:

技术分享
--查询一个表用到的所有触发器:
SELECT * FROM SYSOBJECTS WHERE xtype=TR AND parent_obj=OBJECT_ID(Table_Name)

--查询一个表中所有的列名
SELECT NAME FROM SYSCOLUMNS WHERE ID=OBJECT_ID(Table_Name)

--查询一个表中所有的列名及数据类型
SELECT A.Name AS ColumnNAME,B.Name AS DataType FROM SYSCOLUMNS A 
LEFT JOIN SYSTYPES B ON A.XTYPE=B.xtype WHERE A.ID=OBJECT_ID(Table_Name)

--查询一个表中所有的列名及数据类型(包含表的类型)
SELECT A.Name AS FieldName,B.Type_Desc,B.Type,C.Name AS TypeName FROM SYS.COLUMNS A
LEFT JOIN SYS.OBJECTS B ON A.object_id=B.object_id
LEFT JOIN SYS.TYPES C ON A.system_type_id=C.system_type_id
WHERE B.type=U AND CHARINDEX(UDT,C.name,0)<=0 AND CHARINDEX(SYS,C.name,0)<=0 AND B.name=Table_Name

--读取数据库对象脚本(Only For View/SP/Function/Trigger)
SP_HELPTEXT Obj_Name
View Code

 

SQL Server常见操作

标签:

原文地址:http://www.cnblogs.com/sccd/p/5479064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!