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. day 2014-04-13

    crystal 10:00:40 米多爸爸 11:51:47 很滋润嘛.一般有送股题材的股票都会在送股消息公告之前炒上一阵子,真到了题材兑现就涨不动了,也有些会在除权后走一波填权行情.现在不是牛市,后 ...

  2. nginx负载均衡 - session失效

    最近迷上了Nginx,真实麻雀虽小,五脏俱全..功能实在强大.. nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以 ...

  3. Ubuntu 安装Android Studio与使用手册

    用的是Ubuntu 12.04 1.先去下载,国内可以去这里下载 https://github.com/inferjay/AndroidDevTools 2.下载后解压进入android-studio ...

  4. Export BOM - BOMPXINQ.EXPLODER_USEREXIT API

    --======================================================================== -- Procedure    : explode ...

  5. uva11082 Matrix Decompressing

    网络流 首先算出每行每列的数的和. 每行的值减去c,每列的值减去R 然后每行和每列之间连边,容量为19. 这样一来,(i,j)的流量相当于(i,j)的值-1. 这样就避免了流量为0不对应答案的尴尬情况 ...

  6. NOI2013矩阵游戏

    Description 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的 ...

  7. windows 7环境下配置oracle 11g 客户端

    首先要去官网上下载oracle 11客户端,(我用的是64位系统)地址: http://www.oracle.com/technetwork/database/features/instant-cli ...

  8. Serv-U软件在64位操作系统下使用不了odbc解决方法

    这是因为64位Windows上有两个ODBC连接,你需要创建一个32位的ODBC连接.打开32位ODBC管理器的位置 X:\Windows\syswow64\odbcad32.exe. 利用这个管理器 ...

  9. HTML DOM 教程Part1

    2015-05-08 摘自W3C School HTML DOM HTML DOM 定义了访问和操作HTML文档的标准方法.HTML DOM 把 HTML 文档呈现为带有元素.属性和文本的树结构(节点 ...

  10. Zabbix探索:Zabbix API使用时的错误1

    好久没来了,最近比较疲倦,估计是从年初到现在没有休息的原因. 刚刚批量导入网络设备到Zabbix时,报错: Cannot implode expression 后面自然是IP和触发器的表达式. 最初还 ...