zoukankan      html  css  js  c++  java
  • 获取多表联合查询的存储过程。

    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类型的。
  • 相关阅读:
    近期用过的Linux口令备份
    使用白鹭引擎遇到的一些问题以及一些工具分享
    隐私政策
    又是一年年终总结
    初次数据整理
    skynet的timer似乎有问题
    golang .(type)语法
    mysqlbinlog输出sql
    Mysql中文字符串提取datetime
    ionic4请求skynet服务器的资源跨域问题
  • 原文地址:https://www.cnblogs.com/visibleisfalse/p/3794978.html
Copyright © 2011-2022 走看看