获取多表联合查询的存储过程。

USE [NopV3.3Test]
GO
/****** Object:  StoredProcedure [dbo].[GetPostList]    Script Date: 06/18/2014 15:27:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        于留洋
-- Create date: 2014/06/10
-- Description:    根据筛选条件获取帖子列表
-- =============================================
ALTER PROCEDURE [dbo].[GetPostList] 
    @IsAll bit ,--是否获取所有(0:false;1:true)
    @ForumId int ,--版块id(留言,建议,投诉)
    @CustomerId int ,--客户id
    @PostId int, --内容id
    @TopicId  int --话题id
    
    
AS
BEGIN
  declare @GetDataSql nvarchar(1024); --要执行的sql语句  
  set @GetDataSql='select post.Id, topic.ForumId, post.Text, post.CustomerId,post.IPAddress ,post.CreatedOnUtc from dbo.Forums_Post post';
   set @GetDataSql=@GetDataSql+' inner join dbo.Forums_Topic topic on topic.Id=post.TopicId  where 1=1';
  if(@IsAll=0)
   begin
        if(@ForumId!=0)
            begin
              set @GetDataSql=@GetDataSql+'  and topic.ForumId='+CONVERT(nvarchar(10), @ForumId);
            end
        if(@CustomerId!=0)    
            begin
               set @GetDataSql=@GetDataSql+'  and post.CustomerId='+CONVERT(nvarchar(10), @CustomerId);
            end 
        if(@PostId!=0)
            begin
               set @GetDataSql=@GetDataSql+'  and post.Id='+CONVERT(nvarchar(10), @PostId);
            end
    end 
    --print  @GetDataSql;
exec (@GetDataSql);

END
CONVERT(nvarchar(10), @ForumId),因为forumid是一个int类型,在sql 中不能直接跟字符串进行拼接。所以这个是转换string类型的。

转载于:https://www.cnblogs.com/visibleisfalse/p/3794978.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值