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操作的更多相关文章

  1. 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类

    在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...

  2. Sqlserver日期操作

    Sqlserver日期操作 select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...

  3. SQLserver数据库操作帮助类SqlHelper

    1 SqlHelper源码 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using ...

  4. SQLServer数据操作(建库、建表以及数据的增删查改)

              SQLSever数据操作   一.建立数据库:   create database DB ---数据库名称 (          name=data1 --文件名,         ...

  5. C# 连接 SQLServer 及操作

    随笔:连接: // 将tb_User表数据添加到DataGridView中 string sqlconn = "Data Source=localhost;Initial Catalog=d ...

  6. sqlserver 表操作 SQL篇

    数据库知识点 1.数据库操作: 增:insert into 表名 values(值1,值2,值3) 删:delete 列名 from 表名 where 条件 改:update 表名 set =值 wh ...

  7. (转)SQLServer分区表操作

    原文地址:https://www.cnblogs.com/libingql/p/4087598.html 1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一 ...

  8. SQLServer数据操作(建库、建表以及数据的增删查改)[转]

    SQLSever数据操作   一.建立数据库:   create database DB ---数据库名称 (          name=data1 --文件名,          filename ...

  9. SqlServer知识点-操作xml

    一.开发环境 SQL2010 二.开发过程 1.声明一个xml类型变量 DECLARE @xmlInfo XML; SET @xmlInfo = '<CompanyGroup> <C ...

随机推荐

  1. Java基础知识强化之IO流笔记82:NIO之 Pipe(管道)

    1. Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 2. Pip ...

  2. Java基础知识强化之IO流笔记78:NIO之 FileChannel

    Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...

  3. ASP.NET页面与IIS底层交互和工作原理详解(第二回)

    引言 在 Part.1 Http请求处理流程 一文中,我们了解了Http请求的处理过程以及其它一些运作原理.我们知道Http管道中有两个可用接口,一个是IHttpHandler,一个是IHttpMod ...

  4. js、jquery、动态添加option项

    var selid = document.getElementById("sltid"); for(var i=0; i<10;i++){     //循环添加多个值 sid ...

  5. JavaScript 中的算术运算

    JavaScript中算术运算在溢出(overflow).下溢(underflow)或被零整除时不会报错,当数字运算结果超过了JavaScript所能表示的数字上限(溢出),结果为一个特殊的无穷大(i ...

  6. java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

    ***************************错误提示************************************************ SEVERE: A child cont ...

  7. hdu-5690 All X(快速幂+乘法逆元)

    题目链接: All X Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Pro ...

  8. Backbone.js学习之Router

    官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...

  9. Table of Contents - Git

    Downloading and  Installing Git Download for Linux and Unix Integration with Eclipse Eclipse 提交工程至 G ...

  10. ruby学习--block

    #当前块 class Block def a_method return yield if block_given? 'no block' end end obj=Block.new puts &qu ...