----------2017-01-03 21:56:56------------字符串分隔 start-------------use LDSQLGO CREATE function [dbo].[split]( @str varchar(4500), @sep varchar(1))returns @t table(id int identity(1,1),col varchar(4500))asbegin--分别定义了 目前位置,分隔符开始和字符串长度和,当前获取的字符串declare @posi int,@start int,@str_leg int,@gchar varchar(2),@mingzhong intset @str_leg=len(@str)set @posi=0set @start=0set @mingzhong=0while(@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 returnend Go---------调用egselect * from split('1,2,3,4',',')--字符串分隔 end-------------use LDSQL ----创建库 startcreate 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表goif exists(select * from sysdatabases where name='LDSQL1')drop database LDSQL1go---删库end--创建表startuse LDSQLgo if exists(select * from sysobjects where name='userinfo')drop table userinfocreate 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'--添加账户结束--添加权限 startuse LDSQLgo grant select,update,insert on userinfo to LD grant create table to LDgo--添加权限end--存储过程 start ----1 简单存储过程 startif object_id('usp_getuserinfo_simple') is not nulldrop proc usp_getuserinfo_simpleGo create proc usp_getuserinfo_simpleasselect * from [dbo].[userinfo]--执行exec usp_getuserinfo_simple----1 简单存储过程 end----2 参数输入存储过程 startif object_id('usp_getuserinfo_input') is not nulldrop proc usp_getuserinfo_inputGo create proc usp_getuserinfo_input@Age intasselect * from [dbo].[userinfo] where age=@Age--执行exec usp_getuserinfo_input 21----2 参数输入存储过程 end----3 参数输出output存储过程 startif object_id('usp_getuserinfo_output') is not nulldrop proc usp_getuserinfo_outputGo create proc usp_getuserinfo_output@Age int outputasselect @Age=age from userinfo Go--执行declare @Age intexec usp_getuserinfo_output @Age outputselect @Age ----3 参数输出output存储过程 end----4 参数输入输出output存储过程 startif object_id('usp_getuserinfo_input_output') is not nulldrop proc usp_getuserinfo_input_outputGo create proc usp_getuserinfo_input_output@id int,@Age int outputasselect @Age=age from userinfo where id=@idGo--执行declare @Age intexec usp_getuserinfo_input_output 6,@Age outputselect @Age ----4 参数输入输出output存储过程 end----5 参数输入输出return存储过程 startif object_id('usp_getuserinfo_return') is not nulldrop proc usp_getuserinfo_returnGo Gocreate proc usp_getuserinfo_returnasdeclare @age intbeginselect @age=[age] from [dbo].[userinfo]return @ageendGodeclare @age intexec @age=usp_getuserinfo_returnselect @age ----5 参数输入输出return存储过程 end----6 参数输入输出output_return存储过程 startif object_id('usp_getuserinfo_intput_output_return') is not nulldrop proc usp_getuserinfo_intput_output_returnGo Gocreate proc usp_getuserinfo_intput_output_return@id int,@name char(6)outputasdeclare @age intbeginselect @age=[age],@name=name from [dbo].[userinfo] where id=@idreturn @ageendGodeclare @age intdeclare @name char(6) exec @age=usp_getuserinfo_intput_output_return 6,@name outputselect @age,@name----6 参数输入输出input_output_return存储过程 end--7 分页 start if object_id('usp_select_page') is not null drop proc usp_select_pageGo create proc usp_select_page(@pageIndex int,--当前页码@pagecount int--每页条数)asbeginselect * 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)*@pagecountendexec usp_select_page 0,10--7 分页 end--8 流水号 start -- CREATE TABLE [dbo].[SriaNum] ([Num] [int] NOT NULL)Goif object_id('usp_GetSerialNumber') is not null drop proc usp_GetSerialNumberGocreate PROC usp_GetSerialNumber@SerialNumber VARCHAR(4) OUTPUT -- 指明为输出参数ASIF NOT EXISTS(SELECT * FROM SriaNum) BEGININSERT INTO SriaNum values(1)ENDELSEBEGINUPDATE SriaNum SET Num=Num+1 ENDSELECT @SerialNumber = REPLICATE('0',4-LEN(Num))+CONVERT(VARCHAR(4),Num) --生成[000000001, 999999999]范围内的流水号FROM SriaNumGo --执行 DECLARE @TEST VARCHAR(4)EXECUTE [dbo].usp_GetSerialNumber @TEST OUTPUT -- 指明为输出变量SELECT @TEST AS SERIALNUMBER -- 获得流水号-- --8 流水号 end--9 时间zhuo startGo CREATE FUNCTION UNIX_TIMESTAMP (@ctimestamp datetime) RETURNS integerASBEGIN /* Function body */ declare @return integer SELECT @return = DATEDIFF(SECOND,{d '1970-01-01'}, @ctimestamp) return @returnEND Go CREATE FUNCTION FROM_UNIXTIME (@ts integer) RETURNS datetime ASBEGIN /* Function body */ declare @return datetime select @return = DATEADD(second, @ts, {d '1970-01-01'}) return @returnENDGo Go--9 时间-- end--存储过程 end--随机取出10条数据 startselect top 10 * from [dbo].[userinfo] order by newid() --随机取出10条数据 endGo--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'