34,590
社区成员
发帖
与我相关
我的任务
分享
create PROC sp_FenYe_news
@startIndex int,
@endIndex int,
@where nvarchar(1000),
@count int output
AS
SET NOCOUNT ON
create table #indextable ([id] int identity(1,1),nid int)--声明一个表变量
DECLARE @sql AS NVARCHAR(4000)
SET ROWCOUNT @endIndex
IF(LEN(@where)>0 AND @where is not null)--判断条件是不是为空
BEGIN
SET @sql='INSERT INTO #indextable (nid) SELECT [id] FROM news WHERE '+ @where
EXEC SP_EXECUTESQL @sql--,N'@where',@where
END
ELSE
INSERT INTO #indextable(nid) SELECT [id] FROM news
SELECT @count=COUNT([id]) from #indextable
SELECT * FROM news n
INNER JOIN #indextable t ON n.[id]=t.nid
WHERE t.[nid]/*改了一下*/ between @startIndex and @endIndex ORDER BY t.[id] DESC
SET NOCOUNT OFF
create table news(id int identity(1,1),name varchar(10))
insert into news select 'aaa'
exec sp_FenYe_news 2,6,'id>5',1
id name id nid
----------- ---------- ----------- -----------
6 aaa 1 6
CREATE PROC sp_FenYe_news
@startIndex int,
@endIndex int,
@where nvarchar(1000),
@count int output
AS
SET NOCOUNT ON
DECLARE @indextable table([id] int identity(1,1),nid int)--声明一个表变量
DECLARE @sql AS NVARCHAR(4000)
SET ROWCOUNT @endIndex
IF(LEN(@where)>0 AND @where is not null)--判断条件是不是为空
BEGIN
SET @sql='INSERT INTO @table (nid) SELECT [id] FROM news WHERE'+ @where
EXEC SP_EXECUTESQL @sql,N'@where',@where
END
ELSE
INSERT INTO @indextable(nid) SELECT [id] FROM news
SELECT @count=COUNT([id]) from @indextable
SELECT * FROM news n
INNER JOIN @indextable t ON n.[id]=t.nid
WHERE t.[id] between @startIndex and @endIndex ORDER BY t.[id] DESC
SET NOCOUNT OFF
create table news(id int identity(1,1),name varchar(10))
insert into news select 'aaa'
exec sp_FenYe_news 2,6,'',1
id name id nid
----------- ---------- ----------- -----------
6 aaa 6 6
5 aaa 5 5
4 aaa 4 4
3 aaa 3 3
2 aaa 2 2
--改动地方:
--1.修改表变量为临时表
--2.在SET @sql='INSERT INTO #t (nid) SELECT [id] FROM news WHERE '的WHERE后面加个空格
--3.EXEC SP_EXECUTESQL 后面的@where没有声明类型
--因临时表在过程执行完后会自动消失,所以没有DROP。
EXEC SP_EXECUTESQL @sql,N'@where',@where
==》
EXEC SP_EXECUTESQL @sql
SET NOCOUNT ON
CREATE table #indextable([id] int identity(1,1),nid int)--声明一个表变量
DECLARE @sql AS NVARCHAR(4000)
SET ROWCOUNT @endIndex
IF(LEN(@where)>0 AND @where is not null)--判断条件是不是为空
BEGIN
SET @sql='INSERT INTO #indextable (nid) SELECT [id] FROM news WHERE'+ @where
EXEC SP_EXECUTESQL @sql,N'@where',@where
END
ELSE
INSERT INTO @indextable(nid) SELECT [id] FROM news
SELECT @count=COUNT([id]) from @indextable
SELECT * FROM news n
INNER JOIN @indextable t ON n.[id]=t.nid
WHERE t.[id] between @startIndex and @endIndex ORDER BY t.[id] DESC
SET NOCOUNT OFF
CREATE PROC sp_FenYe_news
@startIndex int,
@endIndex int,
@where nvarchar(1000),
@count int output
AS
SET NOCOUNT ON
create table #t([id] int identity(1,1),nid int)--创建一个临时表
DECLARE @sql AS NVARCHAR(4000)
SET ROWCOUNT @endIndex
IF(LEN(@where)>0 AND @where is not null)--判断条件是不是为空
BEGIN
SET @sql='INSERT INTO #t (nid) SELECT [id] FROM news WHERE '+ @where
EXEC SP_EXECUTESQL @sql,N'@where nvarchar(1000)',@where
END
ELSE
INSERT INTO #t(nid) SELECT [id] FROM news
SELECT @count=COUNT([id]) from #t
SELECT *
FROM news n
INNER JOIN #t t ON n.[id]=t.nid
WHERE t.[id] between @startIndex and @endIndex
ORDER BY t.[id] DESC
SET NOCOUNT OFF
改成临时表:
CREATE PROC sp_FenYe_news
@startIndex int,
@endIndex int,
@where nvarchar(1000),
@count int output
AS
SET NOCOUNT ON
create table #indextable ([id] int identity(1,1),nid int)--声明一个表变量
DECLARE @sql AS NVARCHAR(4000)
SET ROWCOUNT @endIndex
IF(LEN(@where)>0 AND @where is not null)--判断条件是不是为空
BEGIN
SET @sql='INSERT INTO #indextable (nid) SELECT [id] FROM news WHERE'+ @where
EXEC SP_EXECUTESQL @sql,N'@where',@where
END
ELSE
INSERT INTO #indextable(nid) SELECT [id] FROM news
SELECT @count=COUNT([id]) from #indextable
SELECT * FROM news n
INNER JOIN #indextable t ON n.[id]=t.nid
WHERE t.[id] between @startIndex and @endIndex ORDER BY t.[id] DESC
DROP TABLE #indextable
SET NOCOUNT OFF