发布网友 发布时间:2022-04-27 09:49
共5个回答
懂视网 时间:2022-04-30 15:58
--****************************** -- 多级分类存储过程 -- WDFrog 2012-2-15 -- http://wdfrog.cnblogs.com --****************************** --****************************** --数据表定义 --****************************** if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category]‘) and OBJECTPROPERTY(id, N‘IsUserTable‘) = 1) drop table [dbo].[Category] GO CREATE TABLE [dbo].[Category] ( [ClassID] [int] NOT NULL , [ClassName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [Code] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NOT NULL , [DataNum] [int] NULL , [Info] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[Category] ADD CONSTRAINT [DF_Category_DataNum] DEFAULT (0) FOR [DataNum], CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED ( [ClassID] ) ON [PRIMARY] GO --************************* -- 添加分类存储过程 --*************************** if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_Add]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_Add] GO Create Proc Category_Add @ClassName nvarchar(50), @DataNum int , @Info nvarchar(1000), @ParentID int -- 0表示根类别 As Declare @EditCode int Declare @StepLen int Declare @matchStr nvarchar(50) Declare @typeCode nvarchar(50) Declare @Code nvarchar(200) Declare @MyCode nvarchar(200) Declare @ParentCode nvarchar(200) Declare @selfCode int Set @editCode=1 Set @StepLen=4 Set @matchStr=REPLICATE(‘_‘,@StepLen) --4个_ set @typeCode=‘‘ Set @Code=‘‘ Set @MyCode=‘‘ Set @selfCode=0 Set @ParentCode=‘‘ Select @ParentCode=Code From [Category] Where ClassID=@ParentID If(@editCode=1) Begin --获取子类中编号最大的Code,column.ParentCode + matchStr中 Select Top 1 @MyCode= Code From [Category] Where Code Like @ParentCode + @matchStr Order By Code DESC If @@ROWCOUNT >0 Begin Set @selfCode=Cast(Right(@MyCode,@StepLen) As Int ) +1 Set @typeCode=Replicate(‘0‘,@StepLen-1) + Cast(@selfCode As nvarchar) Set @typeCode=Right(@typeCode,@StepLen) Set @typeCode=@ParentCode + @TypeCode End Else Begin Set @typeCode=@ParentCode +Replicate(‘0‘,@StepLen-1)+‘1‘ End End Declare @ClassID int Set @ClassID=0 --获取最大ClassID Select @ClassId=Max(ClassID) From [Category] If Not @ClassID Is Null Begin Set @ClassId=@ClassID +1 End Else Begin Set @ClassID=1 End Insert into [Category] (ClassID,ClassName,Code,DataNum, Info) values (@ClassID,@ClassName,@typeCode,@DataNum, @Info) Select @ClassID As ClassID Go
--******************** -- 修改分类存储过程 --********************* if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_Update]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_Update] GO Create Proc Category_Update @ClassID int , --需要修改的ClassID @ClassName nvarchar(50), @Info nvarchar(1000), @ParentID int As Declare @EditCode int Declare @StepLen int Declare @matchStr nvarchar(50) Declare @typeCode nvarchar(50) Declare @Code nvarchar(200) Declare @MyCode nvarchar(200) Declare @ParentCode nvarchar(200) Declare @selfCode int Set @editCode=0 Set @StepLen=4 Set @matchStr=REPLICATE(‘_‘,@StepLen) --4个_ set @typeCode=‘‘ Set @Code=‘‘ Set @MyCode=‘‘ Set @selfCode=0 Set @ParentCode=‘‘ Select @ParentCode=Code From [Category] Where ClassID=@ParentID Select @Code=Code From [Category] Where ClassID=@ClassID --修改原有类别 --确定是否要修改Code字段 --查看是否改变了直接父类别(上一级) If @ParentCode != Left(@code,len(@code)-@StepLen) Begin --过滤选择自己做为父类 If(@ParentCode !=@Code) Begin --过滤选择自己的子类为父类 If Len(@ParentCode) > Len(@Code) Begin --因为 Len(@ParentCode) > Len(@Code) 所以可以Left(@ParentCode,Len(@Code)) If Left(@ParentCode,Len(@Code)) != @Code --如果相等则为选择自己的子类为父类 Begin Set @EditCode=1 End End Else Begin Set @EditCode=1 End End End If(@editCode=1) Begin --获取子类中编号最大的Code,column.ParentCode + matchStr中 Select Top 1 @MyCode= Code From [Category] Where Code Like @ParentCode + @matchStr Order By Code DESC --是否有子类 If @@ROWCOUNT >0 Begin Set @selfCode=Cast(Right(@MyCode,@StepLen) As Int ) +1 Set @typeCode=Replicate(‘0‘,@StepLen-1) + Cast(@selfCode As nvarchar) Set @typeCode=Right(@typeCode,@StepLen) Set @typeCode=@ParentCode + @TypeCode End Else --没有子类那么编号从1开始 Begin Set @typeCode=@ParentCode +Replicate(‘0‘,@StepLen-1)+‘1‘ End End If (@editCode=1) Begin Update [Category] Set ClassName=@ClassName,Code=@typeCode, Info=@Info where ClassID=@ClassID End Else Begin Update [Category] Set ClassName=@ClassName, Info=@Info where ClassID=@ClassID End ---修改子类编号(Code) If(@editCode=1) Begin Update [Category] Set Code=@typeCode + Right(Code,Len(Code)-Len(@Code)) Where Code Like @Code + ‘%‘ End GO --************************************ -- 删除一个分类,只允许删除没有子类的分类 --************************************ if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_Del]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_Del] GO Create Proc Category_Del @ClassID int As If (Select Count(ClassID) From[Category] Where Code Like(Select Code From [Category] Where ClassID=@ClassID)+‘%‘ And ClassId <> @ClassId ) >0 Begin RaisError (‘不能删除带有子类的分类‘,16,1) Return End Declare @Code nvarchar(200) Declare @Value int Set @Value=0 Select @Code=[Code],@Value=[DataNum] From [Category] Where [ClassID]=@ClassID Update [Category] Set [DataNum]=[DataNum] - @Value Where [ClassID] In( Select ClassID From [Category] Where Len(Code)<=Len(@Code) And Code=Left(@Code,Len(Code))) Delete From Category Where ClassID=@ClassID Go --************************** -- 根据编号获取一条分类记录 --*************************** if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_Select]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_Select] GO Create PROCEDURE Category_Select @ClassID int AS SELECT [ClassID],[ClassName],[Code],[DataNum], [Info] FROM [Category] WHERE [ClassID]=@ClassID Go --************************** -- 移动分类的排序 --******************************* if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_Move]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_Move] Go Create Proc Category_Move @ClassID int, @IsUp bit=1 As Declare @maskStr nvarchar(200) Declare @tempStr nvarchar(200) Declare @Code nvarchar(200) Set @Code=‘‘ Set @tempStr=‘‘ Select @Code=Code From [Category] Where ClassID=@ClassID Set @maskStr=REPLICATE(N‘-‘,Len(@Code)) If @Code !=‘‘ And ( (Len(@Code) % 4) =0 ) Begin If(@isUp=1) Begin If(Len(@Code) > 4) Begin Select Top 1 @tempStr=Code From [Category] Where Len(Code)=Len(@Code) And Code < @Code And Left(Code,Len(Code)-4)=Left(@Code,Len(@Code)-4) Order By Code DESC End Else Begin Select Top 1 @tempStr=Code From [Category] Where Len(Code)=Len(@Code) And Code < @Code Order By Code DESC End End Else Begin If(Len(@Code) >4) Begin Select Top 1 @tempStr=Code From [Category] Where Len(Code)=Len(@Code) And Code > @Code And Left(Code,Len(Code)-4)=Left(@Code,Len(@Code)-4) Order By Code ASC End Else Begin Select Top 1 @tempStr=Code From [Category] Where Len(Code)=Len(@Code) And Code >@Code Order By Code ASC End End End -- //已经是最前(最后) If @tempStr Is Null Or RTrim(LTrim(@tempStr))=‘‘ Begin return End Declare @CodeLen int Declare @MAXLEN int Set @CodeLen=Len(@Code) Set @MAXLEN=200 --//设置目标类,以及目标类的子类为----0001(目标类)或----00010002(子类)为形式 Update [Category] Set Code=@maskStr +Substring(code,@CodeLen +1,@MAXLEN) Where Left(code,@CodeLen)=@tempStr --//更新当前交换类(包括子类)Code为目标类Code Update [Category] Set Code=@tempStr +Substring(Code,@CodeLen+1,@MAXLEN) Where Left(code,@CodeLen)=@Code --//更新目标类(包括子类)Code为当前交换类Code Update [Category] Set Code=@Code +Substring(code,@CodeLen +1,@MAXLEN) Where Left(code,@CodeLen)=@maskStr Go --**************************** --获取指定分类的父分类信息 --***************************** if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_QueryParent]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_QueryParent] Go Create Proc Category_QueryParent @ClassID int As Declare @ClassCode nvarchar(200) Select @ClassCode=Code From [Category] Where ClassId=@ClassID Select ClassID,ClassName,Code, DataNum From [Category] Where Len(Code)<=Len(@ClassCode) And Code = Left(@ClassCode,Len(Code)) Order By Code Go --****************************** -- 获取整个分类目录 --****************************** if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Category_Query]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1) drop procedure [dbo].[Category_Query] Go Create Proc Category_Query As热心网友 时间:2022-04-30 13:06 CREATE PROCEDURE [sp_update] --存储过程名称
(
@param1 nvarchar(50),
@param2 nvarchar(50),
@param3 nvarchar(50),
@param4 nvarchar(50)--这是你传入的参数
)
AS
--更新
UPDATE [你的表名]
SET [表中的字段名1] = @param1,
[表中的字段名2] = @param2 --要更新的字段
WHERE [表中的字段名3] = @param3 and
[表中的字段名4] = @param4 --更新的条件
--返回更新的记录
SELECT * FROM [你的表名]
WHERE [表中的字段名3] = @param3 and
[表中的字段名4] = @param4 --已更新的记录
将以上[]的内容改为你自己的表和字段名, 复制到SQL企业管理器的查询窗口, F5运行即可生成一个存储过程. 同样在SQL企业管理器的查询窗口输入 exec [你的存储过程名称] '参数1', '参数2', '参数3', '参数4'即可执行上面的那条存储过程.热心网友 时间:2022-04-30 14:24
create proc test //创建一个名为test的存储过程
@tablename nvarchar(50)
as
set @tablename='select * from '+@tablename;
execute sp_executesql @tablename;//用系统的sp_executesql存储过程执行SQL语句
print @tablename;
go
c#代码:
cmd=con.CreateCommand();
cmd.Parameters.Add("@tablename",SqlDbType.NVarChar);//指定存储过程的参数
cmd.CommandText="test";//指定存储过程的名称
cmd.CommandType=CommandType.StoredProcere;//指定要执行的类型(存储过程)
cmd.Parameters["@tablename"].Value = tablename;//指定存储过程参数需要传递的值
da=new SqlDataAdapter(cmd);//执行
ds=new DataSet();
da.Fill(ds,"e");//填充数据集
dataGrid1.DataSource=ds.Tables["e"];//绑定数据集热心网友 时间:2022-04-30 15:58
create procere [proc_Name] --定义存储过程
@userName varchar(50) --定义传入量(可以没有)
as
--加入操作语句
--update|insert|delete|等等
select * from inserted --返回更新后的该条记录热心网友 时间:2022-04-30 17:50
同是菜鸟 等待答案