----------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 记录的更多相关文章

  1. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十一)数据层优化-druid监控及慢sql记录

    本文提要 前文也提到过druid不仅仅是一个连接池技术,因此在将整合druid到项目中后,这一篇文章将去介绍druid的其他特性和功能,作为一个辅助工具帮助提升项目的性能,本文的重点就是两个字:监控. ...

  2. PL/SQL 记录 Record 简介

    记录类型是表中当行数据结构的一个镜像.每个记录只存储一行数据,记录包含的是字段,而不是列. 1.用%rowtype属性隐式定义记录类型 declare individual individuals%r ...

  3. SpringBoot配置 druid 数据源配置 慢SQL记录

    spring: datasource: url: jdbc:mysql://127.0.0.12:3306/test?autoReconnect=true&useUnicode=true&am ...

  4. SQL记录-PLSQL记录

    PL/SQL记录   PL/SQL记录就是可以容纳不同类型的数据项的数据结构.记录由不同字段,类似于数据库表的行. 例如,要保留跟踪图书馆中的书籍.可能要跟踪有关每本书下面的属性类似:标题,作者,主题 ...

  5. MySQL 查看执行的SQL记录

    我们时常会有查看MySQL服务端执行的SQL记录.在MySQL5.1之后提供了支持,通过在启动时加入-l 或者--log选项即可: mysqld -l mysqld --log 在后面的版本(5.1. ...

  6. mysql如何配置sql记录

    原文链接:http://www.qqdeveloper.com/detail/11/1.html 为什么要记录sql记录 主要目的是为了检测我们的网站安全问题,有效的避免一些sql注入或者是xss攻击 ...

  7. Oracle pl/sql 记录、表类型

    一.PL/SQL记录 定义: TYPE <类型名> IS RECORD <列名1 类型1,列名2 类型2,...列名n 类型n,> [NOT NULL] <列的类型> ...

  8. druid监控及慢sql记录

    本文提要 前文也提到过druid不仅仅是一个连接池技术,因此在将整合druid到项目中后,这一篇文章将去介绍druid的其他特性和功能,作为一个辅助工具帮助提升项目的性能,本文的重点就是两个字:监控. ...

  9. 怎么打开/查看MySQL的SQL记录

    mysql在执行sql的时候会在日志当中记录很多信息,当然包括执行的所有语句.下面以使用navicat for mysql为例,来展示一下如何打开/查看MySQL的SQL记录: 打开navicat f ...

  10. PL/SQL 记录集合IS TABLE OF的使用

    在PL/SQL代码块中使用select into 赋值的话,有可能返回的是一个结果集.此时,如果使用基本类型或自定义的记录类型,将会报错. 因此,需要定义一个变量,是某种类型的集合.下面以一个基于表的 ...

随机推荐

  1. createjs学习二之flash转canvas学习1

    设置发布 或者 导出参数设置 c_lib_main c_images_main/ 导出之后的文件 打开导出的html文件可以看到如下,,可以正常显示的,,只是stop了还不能动 给舞台上某一元素添加点 ...

  2. java 之return

    return关键词有两个用法,一方面制定一个方法返回什么值,另一方面导致当前方法退出.

  3. NuGet控制台有几个常用命令

    NuGet控制台有几个常用命令 Get-Package 获取当前项目已经安装的类库 Install-Package 安装指定类库,命令格式如下:Install-Package 类库ID,如Instal ...

  4. 给php添加ssl证书

    composer下载时报错: The "https://packagist.org/packages.json" file could not be downloaded: SSL ...

  5. 基于Java Mina框架的部标808服务器设计和开发

    在开发部标GPS平台中,部标808GPS服务器是系统的核心关键,决定了部标平台的稳定性和行那个.Linux服务器是首选,为了跨平台,开发语言选择Java自不待言. 我们为客户开发的部标服务器基于Min ...

  6. linux------------centos防火墙

    CentOS7默认的防火墙不是iptables,而是firewalle. 你可以用rpm -qa | grep iptables来查看,一般会出现两个一个是iptables 另一个是iptables. ...

  7. Android -- 常见控件的小效果

    1,EditText控件 ① 修改光标颜色 自定义drawable 创建cursor.xml文件 <?xml version="1.0" encoding="utf ...

  8. linux格式批量转换为dos格式

    注:写的只是基本知识,望高手勿喷,写这个不是为了炫耀,只是为了方便其他人,仅此而已. 一:脚本功能: 批量处理目录以及子目录下的文件格式问题,能够轻易的将linux格式转换为dos格式. 二:写此博客 ...

  9. matlab GUI界面编程总结

    去年做了一些关于Matlab GUI的程序,现在又要做相关的东西,回想起来,当时很多经验没有记录下来,现在回顾起来始终觉得不爽,所以从现在开始,一定要勤写记录. 从简单的例子说起吧. 创建Matlab ...

  10. Delphi Json

    superobject.pas单元对json的解析非常方便, 力荐 下面演示对如下json的解析 { ", "memo": "S.H.E 004" } ...