CREATE PROCEDURE sp_News_GetNews
@CategoryID int,
@CurrentApprovedOnly bit,
@AbstractLength int
AS-- if @AbractLegth is not -1 return the first @AbstractLength chars of the Body field, otherwise return the whole news body
IF @AbstractLength <> -1
BEGIN
IF @CurrentApprovedOnly = 1
BEGIN
SELECT NewsID, Title, LEFT(CAST(Body AS varchar(1000)), @AbstractLength)  + '...' AS Abstract, ReleaseDate, ExpireDate, AddedDate, Approved, News_News.UserID, (FirstName + ' ' + LastName) AS UserName, EmailAddress AS UserEmail
FROM News_News LEFT JOIN Accounts_Users
ON News_News.UserID = Accounts_Users.UserID
WHERE CategoryID = @CategoryID AND Approved = 1 AND ReleaseDate <= GETDATE() AND ExpireDate >= GETDATE()
END
ELSE
BEGIN
SELECT NewsID, Title, LEFT(CAST(Body AS varchar(1000)), @AbstractLength)  + '...' AS Abstract, ReleaseDate, ExpireDate, AddedDate, Approved, News_News.UserID, (FirstName + ' ' + LastName) AS UserName, EmailAddress AS UserEmail
FROM News_News LEFT JOIN Accounts_Users
ON News_News.UserID = Accounts_Users.UserID
WHERE CategoryID = @CategoryID
END
END
ELSE
BEGIN
IF @CurrentApprovedOnly = 1
BEGIN
SELECT NewsID, Title, Body AS Abstract, ReleaseDate, ExpireDate, AddedDate, Approved, News_News.UserID, (FirstName + ' ' + LastName) AS UserName, EmailAddress AS UserEmail
FROM News_News LEFT JOIN Accounts_Users
ON News_News.UserID = Accounts_Users.UserID
WHERE CategoryID = @CategoryID AND Approved = 1 AND ReleaseDate <= GETDATE() AND ExpireDate >= GETDATE()
END
ELSE
BEGIN
SELECT NewsID, Title, Body AS Abstract, ReleaseDate, ExpireDate, AddedDate, Approved, News_News.UserID, (FirstName + ' ' + LastName) AS UserName, EmailAddress AS UserEmail
FROM News_News LEFT JOIN Accounts_Users
ON News_News.UserID = Accounts_Users.UserID
WHERE CategoryID = @CategoryID
END
END

解决方案 »

  1.   

    if @AbractLegth is not -1 return the first @AbstractLength chars of the Body field, otherwise return the whole news body请教这段说明是什么意思?SELECT NewsID, Title, LEFT(CAST(Body AS varchar(1000)), @AbstractLength)  + '...' AS Abstract, ReleaseDate, ExpireDate, AddedDate, Approved, News_News.UserID, (FirstName + ' ' + LastName) AS UserName, EmailAddress AS UserEmail
    FROM News_News LEFT JOIN Accounts_Users
    ON News_News.UserID = Accounts_Users.UserID
    WHERE CategoryID = @CategoryID AND Approved = 1 AND ReleaseDate <= GETDATE() AND ExpireDate >= GETDATE()这一段是什么意思? 中间的LEFT(CAST(Body AS varchar(1000)), @AbstractLength)  + '...' AS Abstract  和 (FirstName + ' ' + LastName) AS UserName 是什么意思?
      

  2.   

    如果@AbractLegth 这个参数不等于 -1 那么返回 body的第一个@AbstractLength 字符, 否则返回整条新闻
    至于那条sql语句, 你可以查下 left函数, 和cast函数!
      

  3.   

    --if @AbractLegth is not -1 return the first @AbstractLength chars of the Body field, otherwise return the whole news body这句是注释呀,如果@AbractLegth !=-1返回@AbractLegth 体的第一个字符,其他的返回全部第二个是一个左连接的问题,你打开sql帮助看看!
      

  4.   

    -- if @AbractLegth is not -1 return the first @AbstractLength chars of the Body field, otherwise return the whole news body这 是一段注释
      

  5.   

    cast 转换函数  left 从字符串左边开始指定个数的字符
      

  6.   

    cast 转换函数  left 从字符串左边开始指定个数的字符
      

  7.   

    是错在那?语法错还你的SQL语句有问题?
    这样看好象没什么问题呀
      

  8.   

    1、
    -- if @AbractLegth is not -1 return the first @AbstractLength chars of the Body field, otherwise return the whole news body这段话前面加了"--"表示注释,不执行2、
    这一段是什么意思? 中间的LEFT(CAST(Body AS varchar(1000)), @AbstractLength)  + '...' AS Abstract  和 (FirstName + ' ' + LastName) AS UserName 是什么意思?CAST(Body AS varchar(1000))  把字段Body转换为Varchar型
    Left(LEFT(CAST(Body AS varchar(1000)), @AbstractLength))  从左开始取字段body的@AbstractLength长度的内容(CAST(Body AS varchar(1000)), @AbstractLength)  + '...' 把取出的内容后面再加上"..."(FirstName + ' ' + LastName) AS UserName  把字段FirstName和字段LastName连接起来用空格_隔开.(AS UserName UserName为取出内容的别名)
      

  9.   


    如果@AbractLegth !=-1返回@AbractLegth 体的第一个字符,其他的返回全部Left(LEFT(CAST(Body AS varchar(1000)), @AbstractLength))  从左开始取字段body的@AbstractLength长度的内容