VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之存储过程

  • 2019-03-21 14:02 来源:未知

create database UsersInfoDB
use UsersInfoDB
if OBJECT_ID(N'Users',N'U') is not null
print '存在'
else
print '请创建'
if OBJECT_ID(N'UsersRole',N'U') is not null
print '存在'
else
print '请创建'
if OBJECT_ID(N'UsersAndRole',N'U') is not null
print '存在'
else
print '请创建'
if exists(select * from sys.tables where name='Users')
print '已经有表结构'
else
print '请创建'
if exists(select * from sys.tables where name='UsersRole')
print '已经有表结构'
else
print '请创建'
if exists(select * from sys.tables where name='UsersAndRole')
print '已经有表结构'
else
print '请创建'
create table Users
(
UsersID int primary key identity(1,1),
UsersName nvarchar(30) not null,
UsersPwd nvarchar(30) not null,
UsersRolePID int not null,
IsUse nvarchar(30) not null
)
select * from Users
create table UsersRole
(
UsersRoleID int primary key identity(1,1),
UsersRoleName nvarchar(30) not null,
UsersRolePermissionID nvarchar(100) not null,
IsUse nvarchar(30) not null
)
select * from UsersRole
create table UsersAndRole
(
UsersAndRoleID int primary key identity(1,1),
UsersID int,
UsersRoleID int
)
select * from UsersAndRole
select * from UsersRole
select * from Users
insert into UsersRole(UsersRoleName,UsersRolePermissionID,IsUse) values('学生','5,6','启用')
insert into Users(UsersName,UsersPwd,UsersRolePID,IsUse) values('1','1',1,'启用'),('2','2',2,'启用'),('3','3',3,'启用'),('4','4',3,'启用'),('5','5',3,'启用')
insert into UsersAndRole(UsersID,UsersRoleID) values(1,1),(2,2),(3,3),(4,3),(5,3)
select Users.*,UsersRole.UsersRoleName from UsersAndRole
inner join Users on Users.UsersID=UsersAndRole.UsersID
inner join UsersRole on UsersAndRole.UsersRoleID=UsersRole.UsersRoleID where UsersRole.UsersRoleName='老师'
select * from UsersRole where UsersRole.UsersRoleID in (select Users.UsersRolePID from UsersAndRole
inner join Users on Users.UsersID=UsersAndRole.UsersID
inner join UsersRole on UsersAndRole.UsersRoleID=UsersRole.UsersRoleID) 
--1.什么是数据库表分区?
/*把原本存储于一个库的数据分块存储到多个库上,把原本存储于一个表的数据分块存储到多个表上。
*/
--2.数据库设计的3大范式是什么?
/*第一范式
1、每一列属性都是不可再分的属性值,确保每一列的原子性
2、两列的属性相近或相似或一样,尽量合并属性一样的列,确保不产生冗余数据。
第二范式
每一行的数据只能与其中一列相关,即一行数据只做一件事。只要数据列中出现数据重复,就要把表拆分开来。
第三范式
数据不能存在传递关系,即没个属性都跟主键有直接关系而不是间接关系。
*/
select 'YEAR'=YEAR(GETDATE())
select 'MONTH'=MONTH(GETDATE()) 
select 'DAY'=DAY(GETDATE()) 
select 'DATE'=GETDATE() 
create table Course
(
CourseID int primary key identity,
CourseName nvarchar(30) not null
)
insert into Course(CourseName) values('英语'),('数学'),('计算机')
select * from Course
create table Score
(
ScoreID int primary key identity,
Score int,
CoursePID int,
StudentPID int
)
insert into Score(Score,CoursePID,StudentPID) values(89,2,1),(90,1,1),(96,3,1),(91,2,2),(97,1,2),(96,3,2)
select * from Score
create table Student
(
StudentID int primary key identity,
StudentName nvarchar(30) not null,
)
insert into Student(StudentName) values('强强'),('雯雯')
select * from Student
select * from Course
select * from Score

select Student.StudentName as 姓名,
SUM(case when CourseName='英语' then Score.Score else 0 end) as 英语,
SUM(case when CourseName='数学' then Score.Score else 0 end) as 数学,
SUM(case when CourseName='计算机' then Score.Score else 0 end) as 计算机,
SUM(Score.Score) as 总分,(SUM(Score.Score)/COUNT(*)) as 平均分
from Score
inner join Course on Score.CoursePID=Course.CourseID
inner join Student on Score.StudentPID=Student.StudentID
group by Student.StudentName order by 总分 asc
--1.本地缓存的原理是什么?
/*
缓存是分布式系统中的重要组件,主要解决高并发,大数据场景下,热点数据访问的性能问题。提供高性能的数据快速访问。
缓存的原理

将数据写入/读取速度更快的存储(设备);

将数据缓存到离应用最近的位置;

将数据缓存到离用户最近的位置。
*/
--2.什么是读写分离?
/*
为了确保数据库产品的稳定性,很多数据库拥有双机热备功能。也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器;第二台数据库服务器,主要进行读的操作。
*/
--3.什么是队列?
/*
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
*/
use CommodityDbContextDB
-------------------------------
---获取商品地区信息
CREATE PROC UP_GETCommodityDistrict
(
@DistrictPID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT DistrictID,DistrictName,DistrictPID FROM TDistrict WHERE DistrictPID=@DistrictPID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETCommodityDistrict 0
SELECT * FROM TDistrict
-------------------------------
---获取商品类型信息
CREATE PROC UP_GETCommodityType
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT TypeID,TypeName FROM TType 
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETCommodityType
-------------------------------
---添加商品信息
ALTER PROC UP_ADDCommodity
(
@CommodityName NVARCHAR(100),
@NationalityID INT,
@ProvinceID INT,
@CityID INT,
@PlaceOfOrigin NVARCHAR(600),
@IsProduction INT,
@CommodityType NVARCHAR(100),
@FileName NVARCHAR(200)
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
INSERT INTO TCommodity(CommodityName,NationalityID,ProvinceID,CityID,PlaceOfOrigin,IsProduction,CommodityType,[FileName]) VALUES(@CommodityName,@NationalityID,@ProvinceID,@CityID,@PlaceOfOrigin,@IsProduction,@CommodityType,@FileName)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_ADDCommodity 'LED电视',0,5,11,'',1,'电器类','AAA/111'
SELECT * FROM TCommodity
-------------------------------
---显示商品信息
ALTER PROC UP_GETCommodity
(
@CommodityName NVARCHAR(100),
@NationalityID INT,
@pageIndex INT,
@pageSize FLOAT,
@pageCount INT OUTPUT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DECLARE @SQL NVARCHAR(3700),@SQLSTR NVARCHAR(3700)
SET @SQL='SELECT ROW_NUMBER() OVER(ORDER BY CommodityID) AS RowNumber, CommodityID,CommodityName,NationalityID,ProvinceID,CityID,IsProduction,CommodityType,[FileName], PlaceOfOrigin=( TD.DistrictName+'',''+TI.DistrictName+'',''+TDI.DistrictName) FROM TCOMMODITY INNER JOIN TDISTRICT AS TD ON TCOMMODITY.NationalityID=TD.DistrictID INNER JOIN TDISTRICT AS TI ON TCOMMODITY.ProvinceID=TI.DistrictID INNER JOIN TDISTRICT AS TDI ON TCOMMODITY.CityID=TDI.DistrictID WHERE 1=1';
IF(@CommodityName!='')
SET @SQL+=' AND CommodityName LIKE '+'''%'+@CommodityName+'%''';
IF(@NationalityID!=0)
SET @SQL+=' AND NationalityID='+CAST(@NationalityID AS NVARCHAR(30));
SET @SQLSTR='SELECT * FROM ('+@SQL+') AS A WHERE RowNumber BETWEEN '+CAST((@pageIndex-1)*@pageSize+1 AS NVARCHAR(30))+' AND '+CAST(@pageIndex*@pageSize AS NVARCHAR(30));
EXEC(@SQLSTR)
SET NOCOUNT OFF
DECLARE @AllCount FLOAT
EXEC(@SQL)
SELECT @AllCount=@@ROWCOUNT
SET @pageCount=CEILING(@AllCount/@pageSize)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
DECLARE @PageCount INT
EXEC UP_GETCommodity '',0,1,3,@PageCount OUTPUT
SELECT @PageCount
-------------------------------
CREATE PROC UP_DELETECommodity
(
@CommodityID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DELETE FROM TCommodity WHERE CommodityID=@CommodityID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_DELETECommodity 1
-------------------------------
CREATE PROC UP_GETALONECommodity 
(
@CommodityID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT * FROM TCommodity WHERE CommodityID=@CommodityID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETALONECommodity 7
-------------------------------
CREATE PROC UP_ALTERCommodity
(
@CommodityName NVARCHAR(100),
@NationalityID INT,
@ProvinceID INT,
@CityID INT,
@PlaceOfOrigin NVARCHAR(600),
@IsProduction INT,
@CommodityType NVARCHAR(100),
@FileName NVARCHAR(200),
@CommodityID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
UPDATE TCommodity SET CommodityName=@CommodityName,NationalityID=@NationalityID,ProvinceID=@ProvinceID,CityID=@CityID,PlaceOfOrigin=@PlaceOfOrigin,IsProduction=@IsProduction,CommodityType=@CommodityType,[FileName]=@FileName WHERE CommodityID=@CommodityID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_ALTERCommodity '电冰箱,洗衣机,啤酒',3,10,31,'',1,'电器类,饮料类','AAA/111',8

-------------------------------
---通用存储过程分页
--通用存储过程分页
CREATE PROCEDURE procList
--我们需要传入的参数
@strWhere NVARCHAR(max), --查询条件
@PageSize INT, --每页显示多少条内容
@PageIndex INT , --当前页
@TableName NVARCHAR(max), --需要查询的表名
@ColName NVARCHAR(max), --排列用到的列名
--程序返回给用户的数据
@SumCount INT OUT, --共多少条记录
@PageCount INT OUT --共多少页
AS
BEGIN
--第一步:计算共多少条记录
DECLARE @sql_SumCount NVARCHAR(max)
SET @sql_SumCount='SELECT @SumCount=COUNT(*) FROM @TableName '
--判断一下,查询条件是否为空
IF @strWhere <>''
BEGIN
SET @Sql_SumCount=@sql_SumCount+' where '+@strWhere
END
--执行Sql语句
EXEC sys.sp_executesql @sql_SumCount,N'@SumCount int out',@SumCount OUTPUT

--------------------------------------------------------------

--第二步:计算一共有多少页
--定义一个临时变量
DECLARE @temp FLOAT
--首先要判断一下是否存在余数
SET @temp=@SumCount%@PageSize
SET @PageCount=@SumCount/@PageSize
IF @temp <> 0
BEGIN
SET @PageCount=@PageCount+1
END

--------------------------------------------------------------

--第三步:返回结果集
--定义一个变量,用于拼接分页的Sql语句
DECLARE @sql NVARCHAR(MAX)
SET @sql='WITH PageList AS (SELECT *,ROW_NUMBER() OVER(ORDER BY '+@ColName +' ) AS RowsId FROM '+@TableName
--判断查询条件是否为空
IF @strwhere <> ''
BEGIN
SET @sql=@sql+' where '+@strWhere
END
SET @sql=@sql+' ) '
SET @sql=@sql+' SELECT * FROM PageList WHERE RowsId BETWEEN '+STR((@PageIndex-1)*@PageSize+1) + ' AND '+STR(@PageIndex*@PageSize)
END
-------------------------------
USE ScholasticDB
CREATE PROC UP_GETTHobby
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT HobbyID,HobbyName FROM THobby 
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTHobby
-------------------------------
CREATE PROC UP_GETTDistrict
(
@DistrictPID INT 
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT DistrictID,DistrictName,DistrictPID FROM TDistrict WHERE DistrictPID=@DistrictPID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTDistrict 0
-------------------------------
CREATE PROC UP_ADDTScholastic
(
@ScholasticName NVARCHAR(100),
@NationalityID INT,
@ProvinceID INT,
@CityID INT,
@ScholasticGender INT,
@ScholasticHobby NVARCHAR(60),
@FileName NVARCHAR(200)
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
INSERT INTO TScholastic(ScholasticName,NationalityID,ProvinceID,CityID,ScholasticGender,ScholasticHobby,[FileName]) VALUES(@ScholasticName,@NationalityID,@ProvinceID,@CityID,@ScholasticGender,@ScholasticHobby,@FileName)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_ADDTScholastic '强强',1,5,17,1,'足球,篮球,游泳','AAA/111'
SELECT * FROM TScholastic
-------------------------------
ALTER PROC UP_GETTScholastic
(
@ScholasticName NVARCHAR(30),
@NationalityID INT,
@ProvinceID INT,
@CityID INT,
@ScholasticGender INT,
@ScholasticHobby NVARCHAR(60),
@pageIndex INT,
@pageSize FLOAT,
@pageCount INT OUTPUT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DECLARE @SQL NVARCHAR(3700),@SQLSTR NVARCHAR(3700)
SET @SQL='SELECT ROW_NUMBER() OVER(ORDER BY ScholasticID) AS RowNumber, ScholasticID,ScholasticName,NationalityID,ProvinceID,CityID,PlaceOfOrigin=(TDN.DistrictName+'',''+TDP.DistrictName+'',''+TDC.DistrictName),ScholasticGender,ScholasticHobby,[FileName] FROM TScholastic INNER JOIN TDistrict AS TDN ON TScholastic.NationalityID=TDN.DistrictID INNER JOIN TDistrict AS TDP ON TScholastic.ProvinceID=TDP.DistrictID INNER JOIN TDistrict AS TDC ON TScholastic.CityID=TDC.DistrictID WHERE 1=1'
IF(@ScholasticName!='null')
SET @SQL+=' AND ScholasticName LIKE '+'''%'+@ScholasticName+'%'''
IF(@NationalityID!=0)
SET @SQL+=' AND NationalityID='+CAST(@NationalityID AS NVARCHAR(30)) 
IF(@ProvinceID!=0)
SET @SQL+=' AND ProvinceID='+CAST(@ProvinceID AS NVARCHAR(30))
IF(@CityID!=0)
SET @SQL+=' AND CityID='+CAST(@CityID AS NVARCHAR(30))
IF(@ScholasticGender=1 OR @ScholasticGender=0)
SET @SQL+=' AND ScholasticGender='+CAST(@ScholasticGender AS NVARCHAR(30))
IF(@ScholasticHobby!='请选择')
SET @SQL+=' AND ScholasticHobby LIKE '+'''%'+@ScholasticHobby+'%'''
SET @SQLSTR='SELECT * FROM ('+@SQL+') AS A WHERE RowNumber BETWEEN '+CAST((@pageIndex-1)*@pageSize+1 AS NVARCHAR(30))+' AND '+CAST(@pageIndex*@pageSize AS NVARCHAR(30))
EXEC(@SQLSTR)
SET NOCOUNT OFF
DECLARE @AllCount FLOAT
EXEC(@SQL)
SELECT @AllCount=@@ROWCOUNT
SET @pageCount=CEILING(@AllCount/@pageSize)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
DECLARE @pageCount INT
EXEC UP_GETTScholastic '',0,0,0,2,'请选择',1,3,@pageCount OUTPUT
SELECT @pageCount

 

SELECT ROW_NUMBER() OVER(ORDER BY ScholasticID) AS RowNumber, ScholasticID,ScholasticName,NationalityID,ProvinceID,CityID,PlaceOfOrigin=(TDN.DistrictName+','+TDP.DistrictName+','+TDC.DistrictName),ScholasticGender,ScholasticHobby,[FileName] FROM TScholastic INNER JOIN TDistrict AS TDN
ON TScholastic.NationalityID=TDN.DistrictID
INNER JOIN TDistrict AS TDP 
ON TScholastic.ProvinceID=TDP.DistrictID
INNER JOIN TDistrict AS TDC
ON TScholastic.CityID=TDC.DistrictID WHERE 1=1
-------------------------------
CREATE PROC UP_DELETETScholastic
(
@ScholasticID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DELETE FROM TScholastic WHERE ScholasticID=@ScholasticID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_DELETETScholastic 10
-------------------------------
CREATE PROC UP_GETALONEScholastic
(
@ScholasticID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT * FROM TScholastic WHERE ScholasticID=@ScholasticID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETALONEScholastic 11
-------------------------------
ALTER PROC UP_UPDATEScholastic
(
@ScholasticName NVARCHAR(100),
@NationalityID INT,
@ProvinceID INT,
@CityID INT,
@ScholasticGender INT,
@ScholasticHobby NVARCHAR(60),
@FileName NVARCHAR(200),
@ScholasticID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
UPDATE TScholastic SET ScholasticName=@ScholasticName, NationalityID=@NationalityID, ProvinceID=@ProvinceID, CityID=@CityID, ScholasticGender=@ScholasticGender, ScholasticHobby=@ScholasticHobby, [FileName]=@FileName WHERE ScholasticID=@ScholasticID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_UPDATEScholastic '周杰伦',1,5,18,1,'篮球,游戏,跑步','111/AAA',1
SELECT * FROM TScholastic
-------------------------------
CREATE PROC DELETEGROUPTScholastic
(
@ArrayID NVARCHAR(100)
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DECLARE @SQL NVARCHAR(3700)
SET @SQL='';
IF(LEN(@ArrayID)>0)
SET @SQL+='DELETE FROM TScholastic WHERE ScholasticID IN ('+@ArrayID+')';
EXEC(@SQL)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC DELETEGROUPTScholastic '2,3,4,12'
SELECT * FROM TScholastic
-------------------------------
use TaskDB
select * from TNeighborhoods
select * from THousingProperty
select * from THousingState
select * from THousingType
select * from TBuildingStructure
select * from TTenement
-------------------------------
CREATE PROC UP_GETTNeighborhoods
(
@NeighborhoodsPID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT NeighborhoodsID,NeighborhoodsName,NeighborhoodsPID FROM TNeighborhoods WHERE NeighborhoodsPID=@NeighborhoodsPID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTNeighborhoods 0
-------------------------------
CREATE PROC UP_GETTHousingProperty
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT HousingPropertyID,HousingPropertyName FROM THousingProperty 
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTHousingProperty
-------------------------------
CREATE PROC UP_GETTHousingState
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT HousingStateID,HousingStateName FROM THousingState 
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTHousingState
-------------------------------
CREATE PROC UP_GETTHousingType
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT HousingTypeID,HousingTypeName FROM THousingType 
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTHousingType
-------------------------------
CREATE PROC UP_GETTBuildingStructure
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT BuildingStructureID,BuildingStructureName FROM TBuildingStructure 
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETTBuildingStructure
-------------------------------
CREATE PROC UP_AddTTenement
(
@TenementNeighborhoods NVARCHAR(160),
@TenementNumber NVARCHAR(160),
@TenementTier NVARCHAR(160),
@TenementRoom NVARCHAR(160),
@TenementFloorSpace NVARCHAR(160),
@TenementMeterRentArea NVARCHAR(160),
@TenementType NVARCHAR(160),
@TenementStructure NVARCHAR(160),
@TenementLocated NVARCHAR(160),
@TenementLocation NVARCHAR(160),
@TenementProperty NVARCHAR(160),
@TenementState NVARCHAR(160)
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
INSERT INTO TTenement(TenementNeighborhoods,TenementNumber,TenementTier,TenementRoom,TenementFloorSpace,TenementMeterRentArea,TenementType,TenementStructure,TenementLocated,TenementLocation,TenementProperty,TenementState) VALUES(@TenementNeighborhoods,@TenementNumber,@TenementTier,@TenementRoom,@TenementFloorSpace,@TenementMeterRentArea,@TenementType,@TenementStructure,@TenementLocated,@TenementLocation,@TenementProperty,@TenementState)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_AddTTenement '瑞景河畔','瑞景河畔16号','1','117','67.77','67.77','一室一厅一卫','混凝土','瑞景河畔16号楼1-117','城中心','公租房','已配租'
SELECT * FROM TTenement
-------------------------------
CREATE PROC UP_GETTTenement
(
@TenementNeighborhoods INT,
@TenementNumber INT,
@TenementType NVARCHAR(160),
@TenementState NVARCHAR(160),
@TenementLocated NVARCHAR(160),
@PageIndex INT,
@PageSize FLOAT,
@PageCount INT OUTPUT,
@DataCount INT OUTPUT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DECLARE @SQL NVARCHAR(3700),@SQLSTR NVARCHAR(3700)
SET @SQL='SELECT ROW_NUMBER() OVER(ORDER BY T1.TenementID) AS RowNumber, T1.TenementID,T1.TenementNeighborhoods,T1.TenementNumber,(TD1.NeighborhoodsName+'',''+TD2.NeighborhoodsName) AS Neighborhoods,T1.TenementTier,T1.TenementRoom,T1.TenementFloorSpace,T1.TenementMeterRentArea,T1.TenementType,T1.TenementStructure,T1.TenementLocated,T1.TenementLocation,T1.TenementProperty,T1.TenementState FROM TTenement AS T1 INNER JOIN TNeighborhoods AS TD1 ON T1.TenementNeighborhoods=TD1.NeighborhoodsID INNER JOIN TNeighborhoods AS TD2 ON T1.TenementNumber=TD2.NeighborhoodsID WHERE 1=1';
IF(@TenementNeighborhoods!=0)
SET @SQL+=' AND TenementNeighborhoods='+CAST(@TenementNeighborhoods AS NVARCHAR(30))
IF(@TenementNumber!=0)
SET @SQL+=' AND TenementNumber='+CAST(@TenementNumber AS NVARCHAR(30))
IF(@TenementType!='请选择')
SET @SQL+=' AND TenementType LIKE '+'''%'+@TenementType+'%'''
IF(@TenementState!='请选择')
SET @SQL+=' AND TenementState LIKE '+'''%'+@TenementState+'%'''
IF(@TenementLocated!='NULL')
SET @SQL+=' AND TenementLocated LIKE '+'''%'+@TenementLocated+'%'''
SET @SQLSTR='SELECT * FROM ('+@SQL+') AS A WHERE RowNumber BETWEEN '+CAST((@PageIndex-1)*@PageSize+1 AS NVARCHAR(30))+' AND '+CAST(@PageIndex*@PageSize AS NVARCHAR(30))
EXEC(@SQLSTR)
SET NOCOUNT OFF
DECLARE @AllCount FLOAT
EXEC(@SQL)
SELECT @AllCount=@@ROWCOUNT
SET @PageCount=CEILING(@AllCount/@PageSize)
SET @DataCount=@AllCount
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
DECLARE @PageCount INT,@DataCount INT
EXEC UP_GETTTenement 0,0,'请选择','请选择','',1,3,@PageCount OUTPUT,@DataCount OUTPUT
SELECT @PageCount,@DataCount
-------------------------------
CREATE PROC UP_DELETETTenement
(
@TenementID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DELETE FROM TTenement WHERE TenementID=@TenementID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_DELETETTenement
-------------------------------
CREATE PROC UP_DELETEGROUPTTenement
(
@ArrayID NVARCHAR(100)
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DECLARE @SQL NVARCHAR(3700)
SET @SQL='';
IF(LEN(@ArrayID)>0)
SET @SQL+='DELETE FROM TTenement WHERE TenementID IN ('+@ArrayID+')';
EXEC(@SQL)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_DELETEGROUPTTenement 
SELECT * FROM TTenement
-------------------------------
CREATE PROC UP_GETALONETTenement
(
@TenementID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
SELECT * FROM TTenement WHERE TenementID=@TenementID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_GETALONETTenement 6
-------------------------------
CREATE PROC UP_UPDATETTenement
(
@TenementNeighborhoods NVARCHAR(160),
@TenementNumber NVARCHAR(160),
@TenementTier NVARCHAR(160),
@TenementRoom NVARCHAR(160),
@TenementFloorSpace NVARCHAR(160),
@TenementMeterRentArea NVARCHAR(160),
@TenementType NVARCHAR(160),
@TenementStructure NVARCHAR(160),
@TenementLocated NVARCHAR(160),
@TenementLocation NVARCHAR(160),
@TenementProperty NVARCHAR(160),
@TenementState NVARCHAR(160),
@TenementID INT
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
UPDATE TTenement SET TenementNeighborhoods=@TenementNeighborhoods, TenementNumber=@TenementNumber, TenementTier=@TenementTier, TenementRoom=@TenementRoom, TenementFloorSpace=@TenementFloorSpace, TenementMeterRentArea=@TenementMeterRentArea, TenementType=@TenementType,TenementStructure=@TenementStructure,TenementLocated=@TenementLocated,TenementLocation=@TenementLocation,TenementProperty=@TenementProperty,TenementState=@TenementState WHERE TenementID=@TenementID
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
EXEC UP_UPDATETTenement
-------------------------------


SELECT ROW_NUMBER() OVER(ORDER BY T1.TenementID) AS RowNumber, T1.TenementID,T1.TenementNeighborhoods,T1.TenementNumber,(TD1.NeighborhoodsName+','+TD2.NeighborhoodsName) AS Neighborhoods,T1.TenementTier,T1.TenementRoom,T1.TenementFloorSpace,T1.TenementMeterRentArea,T1.TenementType,T1.TenementStructure,T1.TenementLocated,T1.TenementLocation,T1.TenementProperty,T1.TenementState FROM TTenement AS T1 INNER JOIN TNeighborhoods AS TD1 ON T1.TenementNeighborhoods=TD1.NeighborhoodsID INNER JOIN TNeighborhoods AS TD2 ON T1.TenementNumber=TD2.NeighborhoodsID
-------------------------------
use RegisterDB

相关教程