***********按条件查询后分页的存储过程,没分了,求帮助,谢谢***

Crossgate_J Crossgate_J 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


--我的表
CREATE TABLE [dbo].[t_Products](
[ProductID] [nvarchar](50) NOT NULL,--产品编号
[ProductName] [nvarchar](50) NOT NULL,--产品名称
[Producer] [nvarchar](50) NOT NULL,--生产商
[AddTime] [datetime] NOT NULL,--入库时间
[Classify] [nvarchar](50) NOT NULL,--分类
[Price] [decimal](10, 2) NOT NULL,--价格
)


--自己写的存储过程,本人菜鸟,写不来
CREATE PROCEDURE Get_Result
(
@startIndex int,--起始索引
@endIndex int,--结束索引
@docount bit,--标记。0:查出符合条件的产品的详细信息;1:查出符合条件的产品的总数
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围

)
AS
SET NOCOUNT ON
IF(@docount=1)
SELECT COUNT(*) AS Counts FROM t_Products
ELSE
BEGIN
DECLARE @indextable TABLE(id int IDENTITY(1,1),nid nvarchar(50))
SET ROWCOUNT @endIndex
INSERT INTO @indextable(nid) SELECT ProductID FROM t_Products ORDER BY ProductID

DECLARE @strsql nvarchar(4000)
SET @strsql='SELECT * FROM t_Products A,'+@indextable+' B WHERE A.ProductID=B.nid AND B.id BETWEEN '+@startIndex+' AND '+@endIndex+' ORDER BY B.id'

IF @ProductName IS NOT NULL AND @ProductName!=''
BEGIN
SET @strsql = @strsql + ' and ProductName like ''%' + @ProductName + '%'''
END
IF @Classify IS NOT NULL AND @Classify!=''
BEGIN
SET @strsql = @strsql + ' and Classify=''' + @Classify + ''''
END
IF @minPrice IS NOT NULL AND @minPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price>=''' + CONVERT(varchar(30),@minPrice) + ''''
END
IF @maxPrice IS NOT NULL AND @maxPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price<=''' + CONVERT(varchar(30),@maxPrice) + ''''
END
PRINT @strsql
EXEC(@strsql)
END
SET NOCOUNT OFF

---------------------------------------
消息 137,级别 15,状态 2,过程 Get_Result,第 23 行
必须声明标量变量 "@indextable"。




请问这个问题该怎么改?
或者请高手帮我写一个按条件查找结果并分页的存储过程。。
要判断条件是否为空。。
谢谢。。没分了
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
Crossgate_J
疑难问题 22,298 成员 121,727 内容