常用方法



--字符串转换成数字
--CAST("1" AS int)
--CONVERT(int,"1") --截取字符串
SUBSTRING(OccurreAddress,0,7) --拼接字符串,数字和数字字符相加,会按照数字相加
select 'str'+'11'--str11
select '2'+1 --3
select 2+CONVERT(nvarchar(max),2)--4
select '2'+CONVERT(nvarchar(max),2)--22
select 'str'+'11'--str11
select '22'+'11' --2211 --时间相关
Year(getdate()) --当前年
Month(getdate()) --当前月
Day(getdate()) --当前日
select getdate() --2016-04-11 17:36:07.950
select SUBSTRING('2015rr05',5,2)--rr
select getdate()
select Month(getdate())
Datediff(d,时间字段,getdate()) --得到离过现在还剩的天数
select Datediff(DAY,2015-12-01,getdate()) --第2个开始 第3个结束
select CONVERT(Datetime,'20151201')--2015-12-01 00:00:00.000
select CONVERT(Datetime,'2015-12-01')--2015-12-01 00:00:00.000 --select @a=count(1) from tb 此处查询语句中不能@a+=
--我设置一个临时@temp, select @temp=count(1) from tb1
--然后先@a=@temp
--我设置一个临时@temp, select @temp=count(1) from tb2
--然后先@a+=','+@temp
--这个临时变量一直变
declare @temp int
declare @b nvarchar(max) set @temp=1;
set @b=CONVERT(nvarchar(max), @temp )
select @b--1
set @temp=2;
set @b+=','+CONVERT(nvarchar(max), @temp )
select @b--12 --以上代码需要转换类型
--其实我们查询数量的并赋值给@temp的时候,是int
--我们在上面直接声明declare @temp nvarchar(max),就不需要再转了。

代码1 存储过程循环+=



USE [ISFTCMOPSSystem]
GO
/****** Object: StoredProcedure [dbo].[SelectEventCount] Script Date: 04/19/2016 10:47:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--重特大案件SeriousCase
--矛盾纠纷案件 DisputesCase
--师生安全案件 StudentSafetyCase
--护路护线案件 RoadLineProtectiveCase ALTER PROCEDURE [dbo].[SelectEventCount] @DistrictCode varchar(max), --传入
@SC NVARCHAR(max) output,
@DC NVARCHAR(max) output,
@SSC NVARCHAR(max) output,
@RPC NVARCHAR(max) output
as --重特大案件SC
declare @str1 nvarchar(max) declare @RecentMonthCount INT
SET @RecentMonthCount = 0 while (@RecentMonthCount <12)
begin
select @str1= COUNT(1) from ISFTCMOPSSystem.dbo.Info_SeriousCase where
SUBSTRING(OccurreAddress,0,7)=@DistrictCode
and Datediff(MONTH,CONVERT(Datetime,OccurreDate),getdate())=@RecentMonthCount
IF @RecentMonthCount = 0
begin
if @str1 ='0'
set @SC='0'
else
set @SC=@str1
end
else
if @str1 ='0'
set @SC = @SC +',0'
else
set @SC = @SC +','+@str1 select @str1= COUNT(1) from ISFTCMOPSSystem.dbo.Info_DisputesCase where
SUBSTRING(PerIdNumber,0,7)=@DistrictCode
and Datediff(MONTH,CONVERT(Datetime,OccurDate),getdate())=@RecentMonthCount
IF @RecentMonthCount = 0
begin
if @str1 ='0'
set @DC='0'
else
set @DC=@str1
end
else
if @str1 ='0'
set @DC = @DC +',0'
else
set @DC = @DC +','+@str1 select @str1= COUNT(1) from ISFTCMOPSSystem.dbo.Info_StudentSafetyCase where
SUBSTRING(PrinIdNumber,0,7)=@DistrictCode
and Datediff(MONTH,CONVERT(Datetime,OccDate),getdate())=@RecentMonthCount
IF @RecentMonthCount = 0
begin
if @str1 ='0'
set @SSC='0'
else
set @SSC=@str1
end
else
if @str1 ='0'
set @SSC = @SSC +',0'
else
set @SSC = @SSC +','+@str1 select @str1= COUNT(1) from ISFTCMOPSSystem.dbo.Info_RoadLineProtectiveCase where
SUBSTRING(PrinIdNumber,0,7)=@DistrictCode
and Datediff(MONTH,CONVERT(Datetime,OccDate),getdate())=@RecentMonthCount
IF @RecentMonthCount = 0
begin
if @str1 ='0'
set @RPC='0'
else
set @RPC=@str1 end
else
if @str1 ='0'
set @RPC = @RPC +',0'
else
set @RPC = @RPC +','+@str1 set @RecentMonthCount = @RecentMonthCount + 1
end
--矛盾纠纷案件DC
SET @RecentMonthCount = 0 -------------------------------------------------------------------------------- --重特大案件SC
--select @DC=COUNT(1) from ISFTCMOPSSystem.dbo.Info_DisputesCase where SUBSTRING(PerIdNumber,0,7)=@DistrictCode
--select @SSC=COUNT(1) from ISFTCMOPSSystem.dbo.Info_StudentSafetyCase where SUBSTRING(PrinIdNumber,0,7)=@DistrictCode
--select @RPC=COUNT(1) from ISFTCMOPSSystem.dbo.Info_RoadLineProtectiveCase where SUBSTRING(PrinIdNumber,0,7)=@DistrictCode

代码 例子2


ALTER proc [dbo].[SelectSpecialEventCount]
@name nvarchar(100),
@arraySting nvarchar(max) output--因为数据库中没有数组类型,将查询多个表的值,组装成一个字符串,输出类型。
as --重特大案件SeriousCase
--命案防控 MurderPrevention
--矛盾纠纷案件 DisputesCase
--师生安全案件 StudentSafetyCase
--护路护线案件 RoadLineProtectiveCase
declare @temp int
set @temp=0 select @temp= COUNT(1) from ISFTCMOPSSystem.dbo.Info_SeriousCase where CaseName= 'd'
if @temp = 0
begin
set @arraySting='0' --第一,=以后+=
end
else
begin
set @arraySting=CONVERT(nvarchar(100),@temp)--第一,=以后+=
end select @temp= COUNT(1) from ISFTCMOPSSystem.dbo.Info_MurderPrevention where CaseName= @name
if @temp=0
begin
set @arraySting+=',0'
end
else
begin
set @arraySting+=','+CONVERT(nvarchar(100),@temp)
end select @temp+= COUNT(1) from ISFTCMOPSSystem.dbo.Info_DisputesCase where CaseName= @name
if(@temp=0)
begin
set @arraySting+=',0'
end
else
begin
set @arraySting+=','+CONVERT(nvarchar(100),@temp)
end select @temp+= COUNT(1) from ISFTCMOPSSystem.dbo.Info_StudentSafetyCase where CaseName= @name
if(@temp=0)
begin
set @arraySting+=',0'
end
else
begin
set @arraySting+=','+CONVERT(nvarchar(100),@temp)
end select @temp+= COUNT(1) from ISFTCMOPSSystem.dbo.Info_RoadLineProtectiveCase where CaseName= @name
if(@temp=0)
begin
set @arraySting+=',0'
end
else
begin
set @arraySting+=','+CONVERT(nvarchar(100),@temp)
end --调试
--declare @a nvarchar(max)
--exec SelectSpecialEventCount 'd',@a output --调用存储过程 参数外不带()
--print @a
--结果50,0,0,0,0

分页存储过程


USE [ISFTCMOPSSystem]
GO
/****** Object: StoredProcedure [dbo].[selectEventUnion] Script Date: 04/18/2016 16:01:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[selectEventUnion]
@name nvarchar(max),
@PageIndex int, --逗号
@PageSize int,
@PageCount int output
as
declare @Count int;
select @Count=count(1)FROM (
select '重特大案件' as title ,CaseName,SeriousCaseID as Id,'Info_SeriousCase' as tableName from dbo.Info_SeriousCase where CaseName like '%'+@name+'%'
union all
select '命案防控' as title ,CaseName,PreventionID as Id,'Info_MurderPrevention' as tableName from dbo.Info_MurderPrevention where CaseName like '%'+@name+'%'
union all
select '矛盾纠纷案件' as title ,CaseName,DCaseID as Id,'Info_DisputesCase' as tableName from dbo.Info_DisputesCase where CaseName like '%'+@name+'%'
union all
select '师生安全案件' as title ,CaseName,SSCaseID as Id ,'Info_StudentSafetyCase' as tableName from dbo.Info_StudentSafetyCase where CaseName like '%'+@name+'%'
union all
select '护路护线案件' as title ,CaseName,PropeCaseID as Id ,'Info_RoadLineProtectiveCase' as tableName from dbo.Info_RoadLineProtectiveCase where CaseName like '%'+@name+'%'
) as tableUnion;
select @PageCount=ceiling(@Count*1.0/@PageSize); select title,CaseName,Id,tableName from
(select row_number() over (order by tableUnion.Id) as indexNumber, title,CaseName,Id,tableName from
(
select '重特大案件' as title ,CaseName,SeriousCaseID as Id,'Info_SeriousCase' as tableName from dbo.Info_SeriousCase where CaseName like '%'+@name+'%'
union all
select '命案防控' as title ,CaseName,PreventionID as Id,'Info_MurderPrevention' as tableName from dbo.Info_MurderPrevention where CaseName like '%'+@name+'%'
union all
select '矛盾纠纷案件' as title ,CaseName,DCaseID as Id,'Info_DisputesCase' as tableName from dbo.Info_DisputesCase where CaseName like '%'+@name+'%'
union all
select '师生安全案件' as title ,CaseName,SSCaseID as Id ,'Info_StudentSafetyCase' as tableName from dbo.Info_StudentSafetyCase where CaseName like '%'+@name+'%'
union all
select '护路护线案件' as title ,CaseName,PropeCaseID as Id ,'Info_RoadLineProtectiveCase' as tableName from dbo.Info_RoadLineProtectiveCase where CaseName like '%'+@name+'%'
) as tableUnion
)as tableUnionLast
where indexNumber between (@PageIndex-1)*@PageSize+1 and @PageIndex*@PageSize;

Sql Server 常用方法、存储过程备用的更多相关文章

  1. sql server系统存储过程大全

    关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...

  2. 在sql server中建存储过程,如果需要参数是一个可变集合怎么处理?

    在sql server中建存储过程,如果需要参数是一个可变集合的处理 原存储过程,@objectIds 为可变参数,比如 110,98,99 ALTER PROC [dbo].[Proc_totalS ...

  3. 在易语言中调用MS SQL SERVER数据库存储过程方法总结

    Microsoft SQL SERVER 数据库存储过程,根据其输入输出数据,笼统的可以分为以下几种情况或其组合:无输入,有一个或多个输入参数,无输出,直接返回(return)一个值,通过output ...

  4. SQL Server 2008 存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过

    SQL Server 2008 存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过     存储过程 创建存储过程 use pubs --pubs为数据库 go create proc ...

  5. SQL Server中存储过程 比 直接运行SQL语句慢的原因

    问题是存储过程的Parameter sniffing     在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以后每次执行存储过 ...

  6. ADO.NET访问SQL Server调用存储过程带回参

    1,ADO.NET访问SQL Server调用存储过程带回参 2,DatabaseDesign  use northwind go --存储过程1 --插入一条商品 productname=芹菜 un ...

  7. SQL Server 优化存储过程的七种方法

    原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...

  8. SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数

    原文:SQL Server中的CLR编程--用.NET为SQL Server编写存储过程和函数 很早就知道可以用.NET为SQL Server2005及以上版本编写存储过程.触发器和存储过程的,不过之 ...

  9. SQL Server中存储过程比直接运行SQL语句慢的原因

    原文:SQL Server中存储过程比直接运行SQL语句慢的原因 在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1.       存储过程只在创造时进行编译即可,以 ...

  10. 查看SQL SERVER 加密存储过程,函数,触发器,视图

    原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create  PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...

随机推荐

  1. Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题

    4.2 Fresco客户端与服务端的交互(一) 解决Q1问题 从这篇博客开始,我们开始讨论客户端与服务端是如何交互的,这个交互的入口,我们从Q1问题入手(博客按照这样的问题入手,是因为当时我也是从这里 ...

  2. Unity3D入门之GUI基础以及常用GUI控件使用(2)

    1.GUI基础 (1)GUI部分是每帧擦除重绘的,只应该在OnGUI中绘制GUI,按钮:GUILayout.Button(“Hello”); 只读标签:GUILayout.Label() (2)修改控 ...

  3. ASP.NET 数据库访问通用工具

    在工作中,有很多项目已上线后,很多项目的数据库服务器都不会对外开放的,外网想直接访问客户数据库服务器时,可能会出现困难. 这时就需要一个可以查询,更新数据库操作的页面了: 本来用sql语句直接操作数据 ...

  4. 电赛初探(二)——语音采集回放系统

    一.系统结构 1.基本要求 (1)话音/功率放大器增益均可调: (2)带通滤波器:通带为300Hz-3.4kHz : (3)ADC:采样频率f s=8kHz,字长不小于8位: (4)语音存储时间≥10 ...

  5. web应用配置

    tomcat 的 server.html 配置文件 加在</Host>之上 <Context path=”/itcast” docBase=”c:\news” /> path虚 ...

  6. spring实战一:装配bean之注入Bean属性

    内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...

  7. .NET 4.0中的泛型的协变和逆变

    转自:http://www.cnblogs.com/jingzhongliumei/archive/2012/07/02/2573149.html 先做点准备工作,定义两个类:Animal类和其子类D ...

  8. POJ2516 Minimum Cost(最小费用最大流)

    一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...

  9. HDU4057 Rescue the Rabbit(AC自动机+状压DP)

    题目大概是给几个DNA片段以及它们各自的权值,如果一个DNA包含某个片段那么它的价值就加上这个片段的权值,同时包含多个相同DNA片段也只加一次,问长度l的DNA可能的最大价值. 与HDU2825大同小 ...

  10. 详解Adorner Layer(zz)

    首先,千万不要觉得Adorner离你很远,因为最简单的WPF界面也会用到Adorner.在WPF中,下面的几个很常见的功能,都是用Adorner实现的.     1. 光标(caret)     2. ...