USE [GuangHong]
GO
/****** Object: StoredProcedure [dbo].[st_MES_SelInspctDetail] Script Date: 11/23/2015 11:45:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[st_MES_SelInspctDetail] (
@MachineNo varchar(50) = ''
,@BCCode varchar(20)=''
,@CreateDateBegin varchar(19) = ''
,@CreateDateEnd varchar(19)=''
,@in_pagesize smallint =20
,@in_page smallint = 1
)
/*************
exec [st_MES_SelInspctDetail] '216','BC','',''
creator: zhuss
create date: 2014.06.26
Remark: 查询设备点检记录
**************/
as
begin
Create table #tbInspectDetail
(
BillNO varchar(50),
MachineNo varchar(20),
ItemID int,
Result int,
InspectTime datetime
)

declare @InsertRow int
insert into #tbInspectDetail(BillNO,MachineNo,ItemID,Result,InspectTime)
select r.BillNO,r.MachineNo,s.ItemID,s.Result,r.InspectTime
from MES_InspectItemRec r
join MES_InspectItemRecSon s on r.BillNO=s.BillNO
join MES_InspectItem a on a.ItemID=s.ItemID

select @InsertRow=@@ROWCOUNT

select a.ItemID,a.ItemName from MES_InspectItem a
join (select distinct ItemID from MES_InspectItemRecSon) b on a.ItemID=b.ItemID

declare @sql varchar(8000),
@StrWhere varchar(1000),
@StrGroup varchar(200),
@StrDataPage varchar(4000),
@StrOrder varchar(100),
@StrTemp varchar(300)
if (@InsertRow>0)
begin
select @StrWhere=' where 1=1 ',
@StrGroup='group by m.MachineNo,m.InspectTime,c.Name',
@StrOrder=' order by a.Date Desc',
@StrTemp='if OBJECT_ID(''temp..#tbInspectResult'') is not null drop table #tbInspectResult
if OBJECT_ID(''temp..#tbInspectResult2'') is not null drop table #tbInspectResult2'

select @sql=isnull(@sql+',','')
+'max(case m.ItemID when '''+Convert(varchar(5),ItemID)+''' then case when m.Result=1 then ''OK'' when m.Result=2 then ''未开机'' else ''NG'' end else ''NG'' end) '''+Convert(varchar(5),ItemID)+''''
from #tbInspectDetail m group by m.ItemID

print @sql

set @sql=' select m.MachineNo MachineNo,m.InspectTime Date
,c.BCCode,'+@sql+' into #tbInspectResult from #tbInspectDetail m
left join MES_BCDetail c on
c.BCCode=[dbo].[Fn_GetBCCodeByTime](Convert(varchar(19),m.InspectTime,120))
group by m.MachineNo,m.InspectTime,c.BCCode
order by m.InspectTime Desc
select ID=identity(int), * into #tbInspectResult2 from #tbInspectResult a'

if(@MachineNo<>'') set @StrWhere=@StrWhere+' and a.MachineNo like ''%'+@MachineNo+'%'''
if(@BCCode<>'') set @StrWhere=@StrWhere+' and a.BCCode='''+@BCCode+''''
if(@CreateDateBegin<>'') set @StrWhere=@StrWhere+' and a.Date>='''+@CreateDateBegin+''''
if(@CreateDateEnd<>'') set @StrWhere=@StrWhere+' and a.Date<='''+@CreateDateEnd+''''
--分页查询数据
select @StrDataPage=' declare @row_from int, @row_to int,@out_total_rows int
select @out_total_rows= count(*) from #tbInspectResult2
select TotalCount=@out_total_rows
execute st_MES_RS_Pages @out_total_rows, '+convert(varchar(5),@in_pagesize)+','+CONVERT(varchar(5),@in_page)+','+
'@row_from output, @row_to output
select r.*,BCCodeName=c.Name from #tbInspectResult2 r
left join MES_BCDetail c on c.BCCode=r.BCCode
where r.ID between @row_from and @row_to order by ID'
set @sql=@StrTemp+@sql+@StrWhere+@StrDataPage
--select @sql
print @sql
exec (@sql)
end
else
begin
select TotalCount=0
select r.ID,r.MachineNo,r.InspectTime Date,'' 'BCCodeName' from MES_InspectItemRec r where 1=0
end
drop table #tbInspectDetail
end
------------------------------------------------------------------------------------------------------------------------

if OBJECT_ID('temp..#tbInspectResult') is not null drop table #tbInspectResult

if OBJECT_ID('temp..#tbInspectResult2') is not null drop table #tbInspectResult2

select m.MachineNo MachineNo,m.InspectTime Date,c.BCCode,
max(case m.ItemID when '9' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '9',
max(case m.ItemID when '3' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '3',
max(case m.ItemID when '6' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '6',
max(case m.ItemID when '7' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '7',
max(case m.ItemID when '1' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '1',
max(case m.ItemID when '10' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '10',
max(case m.ItemID when '4' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '4',
max(case m.ItemID when '5' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '5',
max(case m.ItemID when '2' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '2',
max(case m.ItemID when '8' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '8'

into #tbInspectResult
from #tbInspectDetail m left join MES_BCDetail c on c.BCCode=[dbo].[Fn_GetBCCodeByTime](Convert(varchar(19),m.InspectTime,120))
group by m.MachineNo,m.InspectTime,c.BCCode
order by m.InspectTime Desc

select ID=identity(int), * into #tbInspectResult2 from #tbInspectResult a where 1=1

declare @row_from int, @row_to int,@out_total_rows int
select @out_total_rows= count(*) from #tbInspectResult2
select TotalCount=@out_total_rows

execute st_MES_RS_Pages @out_total_rows, 20,1,@row_from output, @row_to output

select r.*,BCCodeName=c.Name from #tbInspectResult2 r left join MES_BCDetail c on c.BCCode=r.BCCode
where r.ID between @row_from and @row_to order by ID

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
max(case m.ItemID when '9' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '9',
max(case m.ItemID when '3' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '3',
max(case m.ItemID when '6' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '6',
max(case m.ItemID when '7' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '7',
max(case m.ItemID when '1' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '1',
max(case m.ItemID when '10' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '10',
max(case m.ItemID when '4' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '4',
max(case m.ItemID when '5' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '5',
max(case m.ItemID when '2' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '2',
max(case m.ItemID when '8' then case when m.Result=1 then 'OK' when m.Result=2 then '未开机' else 'NG' end else 'NG' end) '8'

if OBJECT_ID('temp..#tbInspectResult') is not null drop table #tbInspectResult

sql存储过程exec执行字符串select 的区别的更多相关文章

  1. SQL语句-exec执行

    动态sql语句基本语法1 :普通SQL语句可以用Exec执行 eg:   Select * fromtableName        Exec('select * from tableName')   ...

  2. 怎样SQL存储过程中执行动态SQL语句

    MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...

  3. sql server中Set与select的区别

    Set与select的区别 Set select 同时多个变量赋值 不支持 支持 表达式返回多个值时 出错 将返回的最后一个值赋给变量 表达式未返回值 变量被null赋值 变量保持原始值

  4. Python全栈开发-执行字符串形式的语句和字符串形式的表达式方法(即exec和eval方法)

    Python有时需要动态的创造Python代码,然后将其作为语句执行  或  作为表达式计算. exec用于执行存储在字符串中的Python代码.   1. 语句与表达式的区别:表达式是 某事,语句是 ...

  5. Python 执行字符串表达式函数(eval exec execfile)

    eval:计算字符串中的表达式 exec:执行字符串中的语句 execfile:用来执行一个文件 在python 2中exec是语句,在python3中exec变为函数,后面要跟括号.在python3 ...

  6. 执行字符串或注释代码段的方法(eval、exec、execfile)

    eval:计算字符串中的表达式exec:执行字符串中的语句execfile:用来执行一个文件 需注意的是,exec是一个语句,而eval()和execfile()则是内建built-in函数. 1 2 ...

  7. SQL 存储过程入门(事务)(四)

    SQL 存储过程入门(事务)(四)   本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我 ...

  8. SQL 存储过程入门(事务)

    本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我们插入的顺序是先插入A,再插入B表,如果 ...

  9. sql server exec 参数的传递

    来源:https://www.cnblogs.com/guohu/p/11142991.html 1 :普通SQL语句可以用exec执行 Select * from tableName exec('s ...

随机推荐

  1. WPF之小动画一

    定义动画: 直接使用Element进行BeginAnimation DoubleAnimation animation = new DoubleAnimation(); animation.By = ...

  2. P147、面试题26:复杂链表的复制

    题目:请实现ComplexListNode* Clone(ComplexListNode* pHead),复制一个复杂链表.在复杂链表中,每个结点除了有一个m_pNext指针指向下一个结点外,还有一个 ...

  3. Lambda表达式 =>(msdn)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 使用EventLog类写Windows事件日志

    在程序中经常需要将指定的信息(包括异常信息和正常处理信息)写到日志中.在C#3.0中可以使用EventLog类将各种信息直接写入Windows日志.EventLog类在System.Diagnosti ...

  5. 1067. Disk Tree(字符串)

    1067 破题啊  写完发现理解错题意了 子目录下会有跟之前重名的 把输入的字符串存下来 排下序 然后依次找跟上面有没有重的 #include <iostream> #include< ...

  6. 关于广义后缀树(多串SAM)的总结

    之前我们给的SAM的例题,基本上是一个串建SAM的就能做的 如果要建多个串的SAM应该怎么做呢 首先看题,bzoj2780 我一开始的想法是SA以前的弄法,把串拼起来,中间加分隔符做SAM 这题确实可 ...

  7. 原创js模型驱动

    使用ajax方式请求数据,向页面展示一个对象的时候,往往让人头疼的是一大堆 .val()  .text()方法,这样做固然不会出错,但是效率太低 以下提供一个自己编写的Jquery模型驱动插件,正在测 ...

  8. 【转】Android中removeCallbacks失效原因

    原文网址:http://blog.sina.com.cn/s/blog_6714fba70100wtx1.html 在Android开发中会使用Handle的removeCallbacks函数,该函数 ...

  9. ramdisk作为根文件系统的配置

    ramdisk作为根文件系统的配置 http://wenku.baidu.com/link?url=oKj45hKtIANfoVzmoFAmWEvESHiqgWysxPASelpzaXk35gWb1I ...

  10. Parallel stepped for loops in .NET C# z

    ; i < ; i += ) public IEnumerable<int> SteppedIntegerList(int startIndex, int endEndex, int ...