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. Git教程之删除文件(8)

    在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交:

  2. 如何利用python使用libsvm

    一:libsvm包下载与使用:      LIBSVM是台湾大学林智仁(Lin Chih-Jen)副教授等开发设计的一个简单.易于使用和快速有效的SVM模式识别与回归的软件包,他不但提供了编译好的可在 ...

  3. Ant build.xml 批量打渠道包回顾!打第三方jar包总结

    配置: eclipse3.9 + ADT22 + sdk 4.0 eclipse带自动混淆的,不过只有在我们手动创建包的时候,才去打签名,去混淆! 开启混淆这样做吧! 必备文件3个: 当然进行ant打 ...

  4. git svn rebase出现了checksum mismatch的错误

    http://stackoverflow.com/questions/3156744/git-svn-rebase-checksum-mismatch This solution was the on ...

  5. Angularjs checkbox的ng属性

    angularjs 默认给 input[checkbox] 元素定制了一些属性,如: <input type="checkbox" ng-mudel="name&q ...

  6. URAL1501. Sense of Beauty(记忆化)

    链接 dfs+记忆化 对于当前状态虽然满足和差 但如果搜下去没有满足的情况也是不可以的 所以需要记忆化下 #include <iostream> #include<cstdio> ...

  7. [原]Unity3D深入浅出 - 光源组件(Light)

    Unity中提供了四种光源: Directional light: 方向光,类似太阳的日照效果. Point light: 点光源,类似蜡烛. Spotlight: 聚光灯,类似手电筒. Area L ...

  8. LeetCode Product of Array Except Self (除自身外序列之积)

    题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...

  9. (三)学习CSS之opacity 属性

    参考:http://www.w3school.com.cn/cssref/pr_opacity.asp opacity 属性设置元素的不透明级别. 所有浏览器都支持 opacity 属性. 注释:IE ...

  10. vim配色方案

    想更换vim配色方案,需要修改两个文件: 第一个修改是在 /user/share/vim/vim73/colors 添加xxx.vim文件://路径里面有些不是vim73,是vim70或其他 第二个修 ...