在SQLSERVER里面,如何写一个存储过程来创建跟踪?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在SQLSERVER里面,如何写一个存储过程来创建跟踪?相关的知识,希望对你有一定的参考价值。
在SQLSERVER里面,如何写一个存储过程来创建跟踪? 当跟踪停止了的时候,如何写一段代码,能让他自动重新跟踪??
参考技术A 如果不将跟踪结果保存到文件或表中,则当sql
server
profiler打开时可以查看跟踪。但是,在停止跟踪并关闭
sql
server
profiler之后会丢失跟踪结果。为了避免这种丢失跟踪结果的情况,可以在关闭
sql
server
profiler之前单击“文件”菜单上的“保存”来保存结果。
sqlserver 存储过程 返回结果集的 例子
本人不会在 sqlserver 存储过程 返回结果集 ,请大家指教如何写这样的存储过程。
能给出代码的例子吗?
我明白,主要是不知道怎么写
在应用程序或网页程序中用你调用存储过程的这个对象像普通记录集一个调用就好了
如果是在查询分析器中执行,可以在下面的“网格”中直接看到返回的这个结果集 参考技术A SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE AIR_RPT_Report5
( @Year varchar(10)= '2008',
@Month Varchar(10)= '03' ,
@SQLWhere VARCHAR(2000)
)
AS
BEGIN
EXEC('SELECT IB_ID,''AIR'' AS SYSTEMID,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_R,
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_P,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' then BLAMT else 0 end ) as AMT_R_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' then BLAMT else 0 end ) as AMT_P_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_USD_AG,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_USD_AG,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_EUR,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_EUR,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_GBP,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_GBP,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''R'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_R_CNY,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''P'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_P_CNY,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end )-
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_Profit
FROM
( SELECT SUM(AMOUNT) AS BLAMT,SUM(BASE_CUR_AMOUNT) AS AMT,A.CUR_ID,ARP_TYPE,RP_INDICATOR,IB_ID
FROM RP_FRT A
JOIN AIR_BL_PLAN B ON A.BL_ID=B.BL_ID
WHERE BL_TYPE<>''总单'' AND Year(A.INPUT_DATE)='+@Year+' AND month(A.INPUT_DATE)='+@Month +@SQLWhere+
'GROUP BY IB_ID,A.CUR_ID,RP_INDICATOR,ARP_TYPE
) AS B
GROUP BY IB_ID
UNION
SELECT IB_ID,''OCEAN'' AS SYSTEMID,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_R,
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_P,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' then BLAMT else 0 end ) as AMT_R_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' then BLAMT else 0 end ) as AMT_P_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_USD_AG,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_USD_AG,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_EUR,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_EUR,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_GBP,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_GBP,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''R'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_R_CNY,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''P'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_P_CNY,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end )-
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_Profit
FROM
( SELECT SUM(AMOUNT) AS BLAMT,SUM(BASE_CUR_AMOUNT) AS AMT,A.CUR_ID,ARP_TYPE,RP_INDICATOR,IB_ID
FROM RP_FRT A
JOIN OC_BL_PLAN B ON A.BL_ID=B.BL_ID
WHERE BL_TYPE<>''MBL'' AND Year(A.INPUT_DATE)='+@Year+' AND month(A.INPUT_DATE)='+@Month +@SQLWhere+
'GROUP BY IB_ID,A.CUR_ID,RP_INDICATOR,ARP_TYPE
) AS B
GROUP BY IB_ID')
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
满意了吗
其实很简单,就是SELECT
以上是关于在SQLSERVER里面,如何写一个存储过程来创建跟踪?的主要内容,如果未能解决你的问题,请参考以下文章
如何写sqlserver2000存储过程?用于批量录入数据,求解