我的sql 记录
----------2017-01-03 21:56:56----------
--字符串分隔 start-------------
use LDSQL
GO
CREATE function [dbo].[split]
(
@str varchar(4500),
@sep varchar(1)
)
returns @t table(id int identity(1,1),col varchar(4500))
as
begin
--分别定义了 目前位置,分隔符开始和字符串长度和,当前获取的字符串
declare @posi int,@start int,@str_leg int,@gchar varchar(2),@mingzhong int
set @str_leg=len(@str)
set @posi=0
set @start=0
set @mingzhong=0
while(@posi<=@str_leg)
begin
set @gchar=substring(@str,@posi,1)
if(@gchar=@sep)
begin
insert into @t values(substring(@str,@start+1,@posi-@start-1))
set @start=@posi
end
set @posi=@posi+1
end
return
end
Go
---------调用eg
select * from split('1,2,3,4',',')
--字符串分隔 end------------- use LDSQL
----创建库 start
create database LDSQL1
on primary -- 默认就属于primary文件组,可省略
(
/*--数据文件的具体描述--*/
name='LDSQL_data', -- 主数据文件的逻辑名称
filename='D:\LDSQL_data.mdf', -- 主数据文件的物理名称
size=5mb, --主数据文件的初始大小
maxsize=100mb, -- 主数据文件增长的最大值
filegrowth=15%--主数据文件的增长率
)
log on
(
/*--日志文件的具体描述,各参数含义同上--*/
name='LDSQL_log',
filename='D:\LDSQL_log.ldf',
size=2mb,
filegrowth=1mb
)
----创建库 end ---删库 start
use master -- 设置当前数据库为master,以便访问sysdatabases表
go
if exists(select * from sysdatabases where name='LDSQL1')
drop database LDSQL1
go
---删库end
--创建表start
use LDSQL
go
if exists(select * from sysobjects where name='userinfo')
drop table userinfo
create table userinfo
(
id int identity(1,1) primary key,
name char(6) ,
age char(4) ,
address nvarchar(50)
) alter table userinfo add time1 datetime --添加字段
alter table userinfo add timestamp nvarchar(50) --添加字段
alter table userinfo add remark nvarchar(50) --添加字段
ALTER TABLE userinfo DROP COLUMN remark --删除字段
go
--alter table 表名
--add constraint 约束名 约束类型 具体的约束说明
--alter table 表名
--drop constraint 约束名
--alter table stuMarks
--add constraint UQ_stuNo Unique(stuNo)
--alter table stuMarks
--drop constraint UQ_stuNo
--创建表end --插入数据 start
insert into userinfo(name, age, address) values('张三',20,'湖南')
Go
--插入多条
declare @i int
set @i=1;
while @i<101
begin
insert into userinfo(name, age, address,time1) values('张三',20+@i,'湖南',GETDATE())
set @i=@i+1
end
Go --插入数据end
--添加账号 start
/*--添加SQL登录账户--*/
exec sp_addlogin 'LD', 'a.123456' -- 账户名为LD,密码为a.123456
--删除xie账户名
exec sp_droplogin 'LD'
--添加账户结束
--添加权限 start
use LDSQL
go
grant select,update,insert on userinfo to LD
grant create table to LD
go
--添加权限end
--存储过程 start
----1 简单存储过程 start
if object_id('usp_getuserinfo_simple') is not null
drop proc usp_getuserinfo_simple Go
create proc usp_getuserinfo_simple
as
select * from [dbo].[userinfo] --执行
exec usp_getuserinfo_simple
----1 简单存储过程 end
----2 参数输入存储过程 start
if object_id('usp_getuserinfo_input') is not null
drop proc usp_getuserinfo_input
Go
create proc usp_getuserinfo_input
@Age int
as
select * from [dbo].[userinfo] where age=@Age --执行
exec usp_getuserinfo_input 21
----2 参数输入存储过程 end
----3 参数输出output存储过程 start
if object_id('usp_getuserinfo_output') is not null
drop proc usp_getuserinfo_output
Go
create proc usp_getuserinfo_output
@Age int output
as
select @Age=age from userinfo
Go
--执行
declare @Age int
exec usp_getuserinfo_output @Age output
select @Age ----3 参数输出output存储过程 end ----4 参数输入输出output存储过程 start
if object_id('usp_getuserinfo_input_output') is not null
drop proc usp_getuserinfo_input_output
Go
create proc usp_getuserinfo_input_output
@id int,
@Age int output
as
select @Age=age from userinfo where id=@id
Go
--执行 declare @Age int
exec usp_getuserinfo_input_output 6,@Age output
select @Age
----4 参数输入输出output存储过程 end ----5 参数输入输出return存储过程 start
if object_id('usp_getuserinfo_return') is not null
drop proc usp_getuserinfo_return
Go
Go
create proc usp_getuserinfo_return
as
declare @age int
begin
select @age=[age] from [dbo].[userinfo]
return @age
end
Go declare @age int
exec @age=usp_getuserinfo_return
select @age
----5 参数输入输出return存储过程 end
----6 参数输入输出output_return存储过程 start
if object_id('usp_getuserinfo_intput_output_return') is not null
drop proc usp_getuserinfo_intput_output_return
Go
Go
create proc usp_getuserinfo_intput_output_return
@id int,
@name char(6)output
as
declare @age int begin
select @age=[age],@name=name from [dbo].[userinfo] where id=@id
return @age
end
Go declare @age int
declare @name char(6)
exec @age=usp_getuserinfo_intput_output_return 6,@name output
select @age,@name
----6 参数输入输出input_output_return存储过程 end --7 分页 start
if object_id('usp_select_page') is not null
drop proc usp_select_page
Go
create proc usp_select_page
(
@pageIndex int,--当前页码
@pagecount int--每页条数
)
as
begin
select * from (select ROW_NUMBER() over(partition by SameRow order by Id) as Row,* from (select *,1 as SameRow from userinfo )m)o where o.Row between @pageIndex*@pagecount+1 and (@pageIndex+1)*@pagecount
end exec usp_select_page 0,10
--7 分页 end
--8 流水号 start --
CREATE TABLE [dbo].[SriaNum] (
[Num] [int] NOT NULL
)
Go
if object_id('usp_GetSerialNumber') is not null
drop proc usp_GetSerialNumber
Go
create PROC usp_GetSerialNumber
@SerialNumber VARCHAR(4) OUTPUT -- 指明为输出参数
AS
IF NOT EXISTS(SELECT * FROM SriaNum)
BEGIN
INSERT INTO SriaNum values(1)
END
ELSE
BEGIN
UPDATE SriaNum SET Num=Num+1
END SELECT
@SerialNumber = REPLICATE('',4-LEN(Num))+CONVERT(VARCHAR(4),Num) --生成[000000001, 999999999]范围内的流水号
FROM SriaNum Go
--执行
DECLARE @TEST VARCHAR(4)
EXECUTE [dbo].usp_GetSerialNumber @TEST OUTPUT -- 指明为输出变量
SELECT @TEST AS SERIALNUMBER -- 获得流水号
--
--8 流水号 end
--9 时间zhuo start
Go
CREATE FUNCTION UNIX_TIMESTAMP (@ctimestamp datetime) RETURNS integer
AS
BEGIN
/* Function body */
declare @return integer
SELECT @return = DATEDIFF(SECOND,{d '1970-01-01'}, @ctimestamp)
return @return
END
Go CREATE FUNCTION FROM_UNIXTIME (@ts integer) RETURNS datetime
AS
BEGIN
/* Function body */
declare @return datetime
select @return = DATEADD(second, @ts, {d '1970-01-01'})
return @return
END
Go Go
--9 时间-- end --存储过程 end --随机取出10条数据 start
select top 10 * from [dbo].[userinfo] order by newid()
--随机取出10条数据 end
Go
--in 的使用方法
select * from [dbo].[userinfo] where [age] in (23,34,56,55)
select * from [dbo].[userinfo] where [age] not in (23,34,56,55) --查询重复
select * from userinfo where id not in (select max(id) from userinfo group by name,age)
--删除重复
Delete from userinfo where id not in (select max(id) from userinfo group by name,age) --模糊查询
select * from [dbo].[userinfo] where name like '%张%' --日程安排提前五分钟提醒
select * from [dbo].[userinfo] where datediff(minute,time1,getdate())>5 ---================================== --1当前时间戳 获取sql
SELECT DATEDIFF(S,'1970-01-01 00:00:00', GETDATE()) - 8 * 3600
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE()) --js
--Math.round(new Date().getTime()/1000)
--C#
--long a = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
---===========================================
--2时间戳→普通时间sql
SELECT DATEADD(s,1483461862,'1970-01-01 08:00:00') as DTime
SELECT DATEADD(S,1483461862 + 8 * 3600,'1970-01-01 00:00:00') --js
-- var unixTimestamp = new Date(Unix timestamp * 1000)
--然后 commonTime = unixTimestamp.toLocaleString() --3 普通时间 → Unix时间戳
--sql
SELECT DATEDIFF(s, '1970-01-01 08:00:00', '2017-01-04 00:44:22.000')
--js
-- var commonTime = new Date(Date.UTC(year, month - 1, day, hour, minute, second)) ---=====流水号 start=============================== --流水号生成规则:
--1:流水号总长度为22位数
--2:流水号总共分三部分:标头(2位)+ 时间戳(YYYYMMDDHHmmSSsss共17位)+ 随机码(3位)
--举例流水号:SN20150812102400111234
--获取时间戳
select convert(varchar,replace(replace(replace(replace(convert(varchar,getdate(),121),'-',''),':',''),' ',''),'.',''))
--结果:20150703114447613 --获取随机码
select substring(convert(varchar,rand()),3,3)
--结果:813 --获取完整的流水号
SELECT 'SN'+convert(varchar,replace(replace(replace(replace(convert(varchar,getdate(),121),'-',''),':',''),' ',''),'.',''))+substring(convert(varchar,rand()),3,3)
--结果:SN20150703114447613813 ---=====流水号 end===============================
exec xp_cmdshell 'mkdir d:\DB'
我的sql 记录的更多相关文章
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十一)数据层优化-druid监控及慢sql记录
本文提要 前文也提到过druid不仅仅是一个连接池技术,因此在将整合druid到项目中后,这一篇文章将去介绍druid的其他特性和功能,作为一个辅助工具帮助提升项目的性能,本文的重点就是两个字:监控. ...
- PL/SQL 记录 Record 简介
记录类型是表中当行数据结构的一个镜像.每个记录只存储一行数据,记录包含的是字段,而不是列. 1.用%rowtype属性隐式定义记录类型 declare individual individuals%r ...
- SpringBoot配置 druid 数据源配置 慢SQL记录
spring: datasource: url: jdbc:mysql://127.0.0.12:3306/test?autoReconnect=true&useUnicode=true&am ...
- SQL记录-PLSQL记录
PL/SQL记录 PL/SQL记录就是可以容纳不同类型的数据项的数据结构.记录由不同字段,类似于数据库表的行. 例如,要保留跟踪图书馆中的书籍.可能要跟踪有关每本书下面的属性类似:标题,作者,主题 ...
- MySQL 查看执行的SQL记录
我们时常会有查看MySQL服务端执行的SQL记录.在MySQL5.1之后提供了支持,通过在启动时加入-l 或者--log选项即可: mysqld -l mysqld --log 在后面的版本(5.1. ...
- mysql如何配置sql记录
原文链接:http://www.qqdeveloper.com/detail/11/1.html 为什么要记录sql记录 主要目的是为了检测我们的网站安全问题,有效的避免一些sql注入或者是xss攻击 ...
- Oracle pl/sql 记录、表类型
一.PL/SQL记录 定义: TYPE <类型名> IS RECORD <列名1 类型1,列名2 类型2,...列名n 类型n,> [NOT NULL] <列的类型> ...
- druid监控及慢sql记录
本文提要 前文也提到过druid不仅仅是一个连接池技术,因此在将整合druid到项目中后,这一篇文章将去介绍druid的其他特性和功能,作为一个辅助工具帮助提升项目的性能,本文的重点就是两个字:监控. ...
- 怎么打开/查看MySQL的SQL记录
mysql在执行sql的时候会在日志当中记录很多信息,当然包括执行的所有语句.下面以使用navicat for mysql为例,来展示一下如何打开/查看MySQL的SQL记录: 打开navicat f ...
- PL/SQL 记录集合IS TABLE OF的使用
在PL/SQL代码块中使用select into 赋值的话,有可能返回的是一个结果集.此时,如果使用基本类型或自定义的记录类型,将会报错. 因此,需要定义一个变量,是某种类型的集合.下面以一个基于表的 ...
随机推荐
- php,session验证码不一致慢半拍
这种问题遇到过一次,后来忘了怎么解决了,所以做下笔记 输出的$_SESSION['code']之所以比图片慢了‘一帧’,这也纯属正常情况因为输出的图片是一个连接一次调用,而echo $_SESSION ...
- Java局部变量
局部变量是在方法被执行时创建,在方法执行结束时被销毁.局部变量在使用时必须进行赋值操作或被初始化,否则会出现编译错误. 在相互不嵌套的作用域中可以同时声明两个名称,类型相同的局部变量, public ...
- LeetCode Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- CentOS7 配置网卡端口镜像
背景 最近一直在研究旁路监测,需要设置一个源端口镜像给两个目的端口(分别接两个监测设备),无奈ip-com交换机没配置明白,研究下使用软件实现暂时代替. 环境 发行版.内核.iptables版本信息如 ...
- shell中对字符串的处理
1.替换字符串1为字符串2 sed "s/str1/str2/g" 2.获取字符串中的一部分 例:boke-blade 取得boke:sed -e "s/-.*//g&q ...
- Java语言程序设计(基础篇) 第七章 一维数组
第七章 一维数组 7.2 数组的基础知识 1.一旦数组被创建,它的大小是固定的.使用一个数组引用变量,通过下标来访问数组中的元素. 2.数组是用来存储数据的集合,但是,通常我们会发现把数组看作一个存储 ...
- Redis入门指南(第2版) Redis设计思路学习与总结
https://www.qcloud.com/community/article/222 宋增宽,腾讯工程师,16年毕业加入腾讯,从事海量服务后台设计与研发工作,现在负责QQ群后台等项目,喜欢研究技术 ...
- Spring Boot项目中使用jdbctemplate 操作MYSQL数据库
不废话,先来代码 pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- jS正则和WEB框架Django的入门
JS正则 -test 判断字符串是否符合规定的正则表达式 -exec 获取匹配的数据 test的例子: 从上述的例子我们可以看出,如果rep.test匹配到了就返回true,否则返回false exe ...
- 解决Chrome flash过期
解决Chrome flash过期的办法安装Adobe Flash Player PPAPI