我的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('0',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'
原文地址:https://www.cnblogs.com/laopo/p/6247177.html