sql存储过程exec执行字符串select 的区别
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 的区别的更多相关文章
- SQL语句-exec执行
动态sql语句基本语法1 :普通SQL语句可以用Exec执行 eg: Select * fromtableName Exec('select * from tableName') ...
- 怎样SQL存储过程中执行动态SQL语句
MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...
- sql server中Set与select的区别
Set与select的区别 Set select 同时多个变量赋值 不支持 支持 表达式返回多个值时 出错 将返回的最后一个值赋给变量 表达式未返回值 变量被null赋值 变量保持原始值
- Python全栈开发-执行字符串形式的语句和字符串形式的表达式方法(即exec和eval方法)
Python有时需要动态的创造Python代码,然后将其作为语句执行 或 作为表达式计算. exec用于执行存储在字符串中的Python代码. 1. 语句与表达式的区别:表达式是 某事,语句是 ...
- Python 执行字符串表达式函数(eval exec execfile)
eval:计算字符串中的表达式 exec:执行字符串中的语句 execfile:用来执行一个文件 在python 2中exec是语句,在python3中exec变为函数,后面要跟括号.在python3 ...
- 执行字符串或注释代码段的方法(eval、exec、execfile)
eval:计算字符串中的表达式exec:执行字符串中的语句execfile:用来执行一个文件 需注意的是,exec是一个语句,而eval()和execfile()则是内建built-in函数. 1 2 ...
- SQL 存储过程入门(事务)(四)
SQL 存储过程入门(事务)(四) 本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我 ...
- SQL 存储过程入门(事务)
本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我们插入的顺序是先插入A,再插入B表,如果 ...
- sql server exec 参数的传递
来源:https://www.cnblogs.com/guohu/p/11142991.html 1 :普通SQL语句可以用exec执行 Select * from tableName exec('s ...
随机推荐
- Java IO5:序列化与反序列化
一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...
- Use powerful plugins in your vim.
# setup by root wget http://prdownloads.sourceforge.net/ctags/ctags-5.8.tar.gz tar -xzvf ctags-5.8.t ...
- WinCE启动失败的原因与解决办法分析
本文通过一个真实的嵌入式项目进行说明.文中的嵌入式系统用的是ARM处理器+WinCE平台,项目的目的是要把WinCE平台从旧版本移植到WinCE6.0平台上.但结果是这个WinCE系统在启动的时候经常 ...
- 【HDOJ】4363 Draw and paint
看题解解的.将着色方案映射为40*40*5*5*5*5*2个状态,40*40表示n*m,5*5*5*5表示上下左右相邻块的颜色,0表示未着色.2表示横切或者竖切.基本思路是记忆化搜索然后去重,关键点是 ...
- 记一次SSH登陆失败问题的定位
创建用户之后,使用ssh协议登陆提示失败. useradd -d /home/hdp -m hdp -g dba -s /bin/bash 通过命令 ssh -v hdp@127.0.0.1 登陆,查 ...
- vijos1067守望者的逃离
裸的矩阵乘法,我却调了一上午……弱到爆啊…… 不过最终辛苦没有白费,我终于彻底搞懂了 要注意几点: 一.必须构造出前几项 二.用矩阵乘法算法之后还要手工算答案,利用首先算好的前几项 三.想好自己构造的 ...
- [转] Splay Tree(伸展树)
好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...
- 探秘Java虚拟机——内存管理与垃圾回收
本文主要是基于Sun JDK 1.6 Garbage Collector(作者:毕玄)的整理与总结,原文请读者在网上搜索. 1.Java虚拟机运行时的数据区 2.常用的内存区域调节参数 -Xms:初始 ...
- 【转】Android动态改变对 onCreateDialog话框值 -- 不错不错!!!
原文网址:http://www.111cn.net/sj/android/46484.htm 使用方法是这样的,Activity.showDialog()激发Activity.onCreateDial ...
- windows内核初窥(二)-----系统机制
系统机制: windows2000为执行体.内核.设备驱动程序等核心态部分提供了一些基础机制.先让我们看看都有哪些: (1)陷阱调度:包括中断.延迟过程调用(DPC).异步过程调用(APC).异常处理 ...