常用sql语句记录
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语句记录的更多相关文章
- 【SQL】Mysql常用sql语句记录
1.创建用户.赋予权限 CREATE DATABASE scadm DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 's ...
- Mysql 常用 SQL 语句集锦
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- 50个常用SQL语句
50个常用SQL语句 Student(S#,Sname,Sage,Ssex) 学生表 S#学号,主键 Course(C#,Cname,T#) 课程表 C#课程号,主键 SC(S#, ...
- oracle sqlplus及常用sql语句
常用sql语句 有需求才有动力 http://blog.csdn.net/yitian20000/article/details/6256716 常用sql语句 创建表空间:create tables ...
- oracle常用SQL语句(汇总版)
Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...
- 常用SQL语句(增删查改、合并统计、模糊搜索)
转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...
- 常用sql语句整理:mysql
## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`( ... )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...
- 50个常用sql语句 网上流行的学生选课表的例子
50个常用sql语句 建表: --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称 ...
随机推荐
- 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
layout: post title: 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分) author: "luowentaoaa" catalog: tr ...
- RabbitMQ (五) 订阅者模式之分发模式 ( fanout )
前面讲到了简单队列和工作队列. 这两种队列有个非常明显的缺点 : 生产者发送的消息,只能进入到一个队列. 消息只能进入到一个队列就意味着消息只能被一个消费者消费. 尽管工作队列模式中,一个队列中的消息 ...
- 3Sum Smaller -- LeetCode
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 【块状树】bzoj3720 Gty的妹子树
块状树.教程见:http://z55250825.blog.163.com/blog/static/1502308092014163413858/将树按一定大小分块,分成许多子树,在每个子树的根节点记 ...
- 微信小程序退款【证书的使用】
1,官方文档的地址 2,在官方文档中给出了证书使用的链接,如下: [其实只有证书的获取,选择.具体的证书怎么在代码中使用,文档中并没有给出说明] 3,第一步准备请求的参数,里面只有五个是参数是有点特殊 ...
- ASP.NET MVC生命周期介绍(转)
本文以IIS7中asp.net应用程序生命周期为例,介绍了asp.net mvc的生命周期. asp.net应用程序管道处理用户请求时特别强调"时机",对asp.net生命周期的了 ...
- css3的cursor
1.cursor属性参考表 还有zoom-in/zoom-out 还有grab/grabbing 2.css (1)前面的基本上就 .xx { cursor: pointer; } (2)后面两个有兼 ...
- jquery给input标签添加data-options属性
//原生JS实现document.getElementById('startPrice').setAttribute("data-options", "required: ...
- [Android Traffic] android 流量计算方法
android流量简介 流量统计文件:路径/proc/net/dev 打开文件,其中 lo 为本地流量, rmnet0 为3g/2g流量, wlan0 为无线流量. 在/sys/class/net/下 ...
- pgmagick,pil不保存图片并且获取图片二进制数据记录
PIL和pgmagick都是python中图像处理的库,只不过PIL功能更强大 pgmagick和PIL中对数据进行调整后经常需要调用write或者save方法保存图片,然后在读取图片的内容,这样很麻 ...