1、表

--建表
if OBJECT_ID('Student') is not null
create table Student(
ID int identity(1,1) not null,
Name nvarchar(50),
Code nvarchar(50),
flag int default(0)
) --建表时添加约束,表字段定义外键、由函数计算获得
CREATE TABLE [dbo].[A](
[Id] [int] IDENTITY(1,1) NOT NULL primary key,
[ClassId] [int] NOT NULL foreign key references [dbo].[Class] ([Id]) ,
[Name] nvarchar(50),
[InvoiceYear] int,
[InvoiceMonth] int,
[Date] AS (dbo.ToDateFromPeriod(InvoiceYear,InvoiceMonth))PERSISTED,
Memo nvarchar(50) NULL
) --增加列
if not exists(select * from syscolumns where id=OBJECT_ID('Student') and name='ID')
alter table student add id int identity(1,1) not null --添加主键约束
if OBJECT_ID('PK_Student') is not null
ALTER TABLE Student DROP CONSTRAINT [PK_ApprovalMatrix]
GO
ALTER TABLE Student WITH CHECK ADD CONSTRAINT [PK_ApprovalMatrix] PRIMARY KEY CLUSTERED (ID asc)
GO --删除表
DELETE FROM [Student] --清空标识值
DBCC CHECKIDENT (Student, RESEED,0) --插入数据
IF NOT EXISTS(SELECT * FROM [Student] WHERE NAME='Win')
INSERT [Student] ([Code],[Memo]) VALUES (N'CA',N'Simon Deschenes') --更新数据
UPDATE [dbo].[Student] SET Code = '' WHERE NAME='Win'

--insert into要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量
Insert into Table2(field1,field2,...) select value1,value2,... from Table1

--select into,要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。
SELECT vale1, value2 into Table2 from Table1

insert into and select into :http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html

2、函数

2.1 --自定义函数,根据年月返回日期

USE TEST
        GO
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        Create function [dbo].[ToDateFromPeriod](@Year int, @Month int)
        returns datetime
        with SCHEMABINDING
        as
        -- Returns the first of the month for the specified year and month.
        begin
        return dateadd(mm,((@Year-1900)*12)+@Month-1,0)
        end

--调用自定义函数
        select dbo.[ToDateFromPeriod]('2015','1') as result

2.2、表值函数

2.2.1 直接返回表

 create function getp(@id int)
       returns table as
       return select * from [Project] where ClientId=@id

2.2.2 返回自定义表

create function getp()
      returns @t table (id int ,name varchar(50))
      as 
        begin
           insert into @t select p.Id,p.Name from Project p
           return 
        end

3、判断对象是否存在

3.1 判断用户是否存在,并创建

if USER_ID('fas') is null

CREATE USER [fas] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]

其他相关判断参考http://www.cnblogs.com/fumj/archive/2012/07/15/2592558.html

3.2 判断自定义类型是否存在

if type_id('userid') is null

create type [dbo].[userid] from [nvarchar](150) null

4、给角色db_owner添加用户dds

EXEC sp_addrolemember 'db_owner', 'dds'

角色功能https://www.mssqltips.com/sqlservertip/1900/understanding-sql-server-fixed-database-roles/

5、SET NOCOUNT ON

使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息

如果存储过程中包含的一些语句并不返回许多实际的数据,则该设置由于大量减少了网络流量,因此可显著提高性能。

http://blog.csdn.net/thomas_chen/article/details/1660562

6、查询数据库中所有表的名称,及数量

select name from sysobjects where xtype='u'

select count(0) from sysobjects where xtype='u'

7、删除数据库中的所有表

select 'delete from [' + name + ']' from sysobjects where xtype='u'

执行上面sql,然后复制结果再次执行。

常用sql语句记录的更多相关文章

  1. 【SQL】Mysql常用sql语句记录

    1.创建用户.赋予权限 CREATE DATABASE scadm DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 's ...

  2. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  3. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  4. 50个常用SQL语句

    50个常用SQL语句 Student(S#,Sname,Sage,Ssex) 学生表  S#学号,主键 Course(C#,Cname,T#) 课程表          C#课程号,主键 SC(S#, ...

  5. oracle sqlplus及常用sql语句

    常用sql语句 有需求才有动力 http://blog.csdn.net/yitian20000/article/details/6256716 常用sql语句 创建表空间:create tables ...

  6. oracle常用SQL语句(汇总版)

    Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...

  7. 常用SQL语句(增删查改、合并统计、模糊搜索)

    转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...

  8. 常用sql语句整理:mysql

    ## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`(  ...  )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...

  9. 50个常用sql语句 网上流行的学生选课表的例子

    50个常用sql语句 建表: --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称 ...

随机推荐

  1. 51nod 1082 与7无关的数【打表/预处理】

    1082 与7无关的数 题目来源: 有道难题 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 一个正整数,如果它能被7整除,或者它的十进制表示法中某个 ...

  2. UVA12096 集合栈计算机(map和vector实现双射关系+集合的交并运算的STL)

    题目大意: 对于一个以集合为元素的栈,初始时栈为空. 输入的命令有如下几种: PUSH:将空集{}压栈 DUP:将栈顶元素复制一份压入栈中 UNION:先进行两次弹栈,将获得的集合A和B取并集,将结果 ...

  3. luoguP1991无线通讯网

    国防部计划用无线网络连接若干个边防哨所.2种不同的通讯技术用来搭建无线网络.每个边防哨所都要配置无线电收发器:有一些哨所还可以配备卫星电话任意两个配置了一条卫星电话线路的哨所(两边均有卫星电话)均可以 ...

  4. [ZOJ3316]Game

    题意:有一个棋盘,棋盘上有一些棋子,两个人轮流拿棋,第一个人可以随意拿,以后每一个人拿走的棋子与上一个人拿走的棋子的曼哈顿距离不得超过$L$,无法拿棋的人输,问后手能否胜利 把棋子看做点,如果两个棋子 ...

  5. 【树链剖分】【树状数组】【最近公共祖先】【块状树】bzoj3631 [JLOI2014]松鼠的新家

    裸题,树状数组区间修改+单点查询.当然要稍微讨论一下链的左右端点是否修改的情况咯. #include<cstdio> #include<algorithm> #include& ...

  6. Spark1.4远程调试

    1)首先,我们是在使用spark-submit提交作业时,使用 --driver-java-options ”-Xdebug -Xrunjdwp:transport=dt_socket,server= ...

  7. 个人python学习路线记录

    一.入门视频 零基础入门学习Python --小甲鱼 二.博客园 python快速教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778. ...

  8. Android中选项卡功能的实现

    Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: Fra ...

  9. Jetty错误:java.lang.IllegalStateException: Form too large 270468>200000的问题解决

    说明: 1.200000单位为byte,并不是2MB,而是200KB,换算参考:https://calc.itzmx.com/ 2.这个是表单提交后长度超过了200KB造成的,除了表单Form,还有U ...

  10. easyui-validatebox 的简单长度验证

    验证: 页面代码: <form id="invoiceEdit"> <input id="fpdm" name="fpdm" ...