SqlServer ,storedprocedure操作
USE [Role]
GO /*Create a table*/
IF OBJECT_ID ('dbo.Users', 'U') IS NOT NULL
DROP TABLE Users
GO
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NOT NULL,
[Password] [varchar](20) NOT NULL,
[TrueName] [varchar](50) NULL,
[sex] [varchar](2) NULL,
[Birthday] [datetime] NULL,
[Phone] [varchar](20) NULL,
[Email] [varchar](50) NULL,
[CreateTime] [datetime] NULL,
CONSTRAINT [PK_Accounts_Users] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO /*Create procedure about select*/
IF OBJECT_ID('GetUserBy','p') IS NOT NULL
DROP PROCEDURE GetUserBy
GO
CREATE PROCEDURE GetUserBy
@id int
AS
BEGIN
SELECT ID,UserName,Password,TrueName,sex,Birthday,Phone,Email FROM [Users] WHERE [Users].[ID]=@id
END
GO EXECUTE GetUserBy @id=1
GO /*Insert*/
CREATE PROCEDURE Insertuser
@userName varchar(50)
,@password varchar(20)
,@trueName varchar(50)
,@sex varchar(2)
,@birthday datetime
,@phone varchar(20)
,@email varchar(20)
AS
BEGIN
INSERT INTO [Users]([UserName],[Password],[TrueName],[sex],[Birthday],[Phone],[Email],[CreateTime])
VALUES(@userName,@password,@trueName,@sex,@birthday,@phone,@email,GETDATE())
END
GO EXECUTE Insertuser @userName='Admin',@password=''
,@truename='Adminstrator',@sex='男',@birthday='2014-08-31'
,@phone='',@email='a@136.com'
Go
/*Insert user return id*/
CREATE PROCEDURE InsertUserReturenId
@userName varchar(50)
,@password varchar(20)
,@trueName varchar(50)
,@sex varchar(2)
,@birthday datetime
,@phone varchar(20)
,@email varchar(20)
AS
BEGIN
INSERT INTO [Users]([UserName],[Password],[TrueName],[sex],[Birthday],[Phone],[Email],[CreateTime])
VALUES(@userName,@password,@trueName,@sex,@birthday,@phone,@email,GETDATE());
SELECT @@IDENTITY
END
GO EXECUTE InsertUserReturenId @userName='Admin',@password=''
,@truename='Adminstrator',@sex='男',@birthday='2014-08-31'
,@phone='',@email='a@136.com'
Go /*Update*/
CREATE PROCEDURE UpdateUser
@id int
,@password varchar(20)
AS
BEGIN
UPDATE [Users] SET [Password]=@password WHERE [ID]=@id
END
GO EXECUTE UpdateUser @id=15,@password=''
GO
/*Delete*/
CREATE PROCEDURE DeleteUserById
@id int
AS
BEGIN
DELETE FROM [Users] WHERE [ID]=@id
END
GO EXECUTE DeleteUserById @id=1
GO /**Clear table**/ Truncate table [Users]
GO /*loop insert data*/
DECLARE
@userName varchar(50)
,@password varchar(20)
,@trueName varchar(50)
,@sex varchar(2)
,@birthday datetime
,@phone varchar(20)
,@email varchar(20)
,@i int SET @userName='Admin'
SET @password=''
SET @trueName='Adminstrator'
SET @sex='男'
SET @birthday='2014-08-31'
SET @phone=''
SET @email='q@138.com'
SET @i=30 WHILE @i<31
BEGIN
SET @userName='admin'+CONVERT(varchar(2),@i)
INSERT INTO [Users]([UserName],[Password],[TrueName],[sex],[Birthday],[Phone],[Email],[CreateTime])
VALUES(@userName,@password,@trueName,@sex,@birthday,@phone,@email,GETDATE())
SET @i=@i+1
END
GO /*Page and sort*/
CREATE PROCEDURE QueryUserByPage1
@pageSize int
,@currentPage int
,@searchName varchar(50)
,@flag int
AS
DECLARE @startPgae int,@endPage int,@sort varchar(50),@sql varchar(1000)
SET @startPgae=@currentPage * @pageSize
SET @endPage=@startPgae + @pageSize -1
SET @sort=
CASE WHEN @flag=1 THEN 'ID'
WHEN @flag=2 THEN 'UserName'
WHEN @flag=3 THEN 'CreateTime'
END
BEGIN
SELECT ROW_NUMBER() OVER(ORDER BY @sort DESC) AS userId FROM [Users]
WHERE useId BETWEEN @startPgae AND @endPage
END
GO /*Pgae*/
IF OBJECT_ID ('QueryUserByPage', 'p') IS NOT NULL
DROP PROCEDURE QueryUserByPage
GO
CREATE PROCEDURE QueryUserByPage
@pageSize int
,@currentPage int
,@searchName varchar(50)
AS
DECLARE @startPgae int,@endPage int
SET @startPgae=(@currentPage-1) * @pageSize + 1
SET @endPage=@startPgae + @pageSize - 1
SET @searchName='%'+RTRIM(@searchName)+'%'
BEGIN
SELECT * FROM
(
SELECT *, ROW_NUMBER() OVER(ORDER BY ID DESC) AS userId
FROM [Users]
WHERE
[UserName] LIKE @searchName
)
tuser
WHERE tuser.userId BETWEEN @startPgae AND @endPage
END
GO
/*拼接字符串分页*/
CREATE PROCEDURE QueryUserByPage2
@start int
,@end int
AS
DECLARE @sql nvarchar(600)
set @sql='select top '+str(@end-@start+1)+' * from Users where ID not in(select top'+str(@start-1)+' ID from Users)'
BEGIN
execute(@sql)
END
GO QueryUserByPage2 @start=2,@end=10
GO
EXECUTE QueryUserByPage @pageSize=10,@currentPage=1,@searchName=''
GO DECLARE
@name varchar(50)
,@description varchar(50)
,@i int
SET @description='teacher'
SET @i=1
WHILE @i<=10
BEGIN
SET @name = 'teacher' + CONVERT(varchar(2),@i)
INSERT INTO [Roles] VALUES(@name,@description)
SET @i=@i+1
END
GO SELECT * from Roles TRUNCATE table Roles
GO SELECT * FROM [UserRoles]
LEFT JOIN [Roles] ON [UserRoles].[RoleID]=[Roles].[ID]
WHERE [UserRoles].[UserID]=1
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
随机产生
select top 10 * from Users order by newid()
左外连接 (Left Jion):包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行
select * from test1 left join test2 on test1.id = test2.id
右外连接 (Rigt Jion):包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行
select * from test1 right join test2 on test1.id = test2.id
declare @name varchar(50),@flag int
SET @name =
CASE
WHEN @flag = 0 THEN 'Mfg item - not for resale'
WHEN @flag < 50 THEN 'Under $50'
WHEN @flag >= 50 and @flag < 250 THEN 'Under $250'
WHEN @flag >= 250 and @flag < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
IF @cost <= @compareprice
BEGIN
PRINT 'These products can be purchased for less than
$'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
SqlServer ,storedprocedure操作的更多相关文章
- 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类
在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...
- Sqlserver日期操作
Sqlserver日期操作 select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...
- SQLserver数据库操作帮助类SqlHelper
1 SqlHelper源码 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using ...
- SQLServer数据操作(建库、建表以及数据的增删查改)
SQLSever数据操作 一.建立数据库: create database DB ---数据库名称 ( name=data1 --文件名, ...
- C# 连接 SQLServer 及操作
随笔:连接: // 将tb_User表数据添加到DataGridView中 string sqlconn = "Data Source=localhost;Initial Catalog=d ...
- sqlserver 表操作 SQL篇
数据库知识点 1.数据库操作: 增:insert into 表名 values(值1,值2,值3) 删:delete 列名 from 表名 where 条件 改:update 表名 set =值 wh ...
- (转)SQLServer分区表操作
原文地址:https://www.cnblogs.com/libingql/p/4087598.html 1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一 ...
- SQLServer数据操作(建库、建表以及数据的增删查改)[转]
SQLSever数据操作 一.建立数据库: create database DB ---数据库名称 ( name=data1 --文件名, filename ...
- SqlServer知识点-操作xml
一.开发环境 SQL2010 二.开发过程 1.声明一个xml类型变量 DECLARE @xmlInfo XML; SET @xmlInfo = '<CompanyGroup> <C ...
随机推荐
- [经典算法] 排列组合-N元素集合的所有子集(二)
题目说明: 给定一组数字或符号,按照字典序产生所有可能的集合(包括空集合),例如给定1 2 3,则可能的集合为:{}.{1}.{1,2}.{1,2,3}.{1,3}.{2}.{2,3}.{3}. 题目 ...
- LRU在MySQL缓存池的实现
MySQL的InnoDB引擎设置有索引及数据缓存池,其中用到的LRU算法来维持缓存的命中率 这里用到了顺序表list来作为缓冲池,每个数据节点称为block 该算法采用“中点插入法”:当插入一个新bl ...
- 重构8-Replace Inheritance with Delegation(委托替换继承)
继承的误用十分普遍.它只能用于逻辑环境,但却经常用于简化,这导致复杂的没有意义的继承层次.看下面的代码: public class Sanitation{ public String WashHand ...
- Spring Data:企业级Java的现代数据访问技术(影印版)
<Spring Data:企业级Java的现代数据访问技术(影印版)>基本信息原书名:Spring Data:Modern Data Access for Enterprise Java作 ...
- DNS resolving 占用大量日志
公司内部DNS配置好后,测试解析正常,只是几乎每秒都有无法解析的日志产生.但分析日志时发现,每天的日志都有2G左右.日志中几乎全是类似network unreachable resolving 'ww ...
- JdbcTemplate 、NamedParameterJdbcTemplate、SimpleJdbcTemplate的区别
一.JdbcTemplate 首先在配置文件中设置数据源 <bean id="dataSource" class="org.springframework.jdbc ...
- Codeforces Round #197 (Div. 2) C,D两题
开了个小号做,C题一开始看错范围,D题看了半小时才看懂,居然也升到了div1,囧. C - Xenia and Weights 给出一串字符串,第i位如果是1的话,表示有重量为i的砝码,如果有该种砝码 ...
- BZOJ 3713
Description 斐波那契数列的定义为:k=0或1时,F[k]=k:k>1时,F[k]=F[k-1]+F[k-2].数列的开头几项为0,1,1,2,3,5,8,13,21,34,55,…你 ...
- spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象
最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...
- android 使用NinePatch图作Background,导致布局混乱
在Layout中使用NinePatch图片作为其布局的背景图片时,可能会导致Layout中的布局发生混乱,此时将此Layout设置一个属性:android:padding="0dp" ...