将此sql sript转换为存储过程

时间:2015-06-17 00:43:09

标签: c# sql-server stored-procedures webforms web-application-project

我在这里有一些脚本(sql命令),我想用作存储过程..任何人都可以帮助..

我想要转换此脚本的原因是为了便于在我的Web应用程序查询中使用。

  /* 1. Select all the duplicates */
  SELECT
        [PTAID]
      ,[RequestID]
      ,[RequestDate]
      ,[ProvName]
      ,[Amount]
      ,[INorOUT]
      ,[Supplier]
      ,[Customer]
      ,[Program]
      ,[IssueDesc]
      ,[Chargeable]
      , COUNT(*) as [Count]
 FROM [PFTracking].[dbo].[TempTable]
GROUP BY
   [PTAID],[RequestID]
      ,[RequestDate]
      ,[ProvName]
      ,[Amount]
      ,[INorOUT]
      ,[Supplier]
      ,[Customer]
      ,[Program]
      ,[IssueDesc]
      ,[Chargeable]
HAVING 
    COUNT(*) > 1

    --2. Add Temp Column

alter table [PFTracking].[dbo].[TempTable] 
   add sno int identity(1,1) 

   -- 3. Remove duplicates 

   delete from [PFTracking].[dbo].[TempTable]
    where sno in(select sno 
    from (
    select *,RANK()
     OVER ( PARTITION BY [PTAID],[RequestID]
      ,[RequestDate]
      ,[ProvName]
      ,[Amount]
      ,[INorOUT]
      ,[Supplier]
      ,[Customer]
      ,[Program]
      ,[IssueDesc]
      ,[Chargeable] ORDER BY sno DESC )rank
     From [PFTracking].[dbo].[TempTable])T 
     where rank>1 )
     alter table [PFTracking].[dbo].[TempTable] drop  column sno

并且可以请任何人帮助我如何在ASP.NET Web App上调用此存储过程?

附加:

    if not exists(select * from sys.servers where name=N'CNCTC-WEB01')
begin

exec sp_addlinkedserver @server='CNCTC-WEB01'

exec sp_addlinkedsrvlogin 'CNCTC-WEB01','false',null,'svc_Phils','Apple@6'

end

INSERT INTO [PFTracking].[dbo].[TempTable]
 SELECT 
    c.[pf_id]
  ,a.[RequestDate]
  ,c.[pf_carrierUsed]
  ,b.[PiecePrice] * b.[PartQuantity] as [Amount]
  ,c.[pf_type]
  ,c.[pf_resSupplier]
  ,c.[pf_resCustomer]
  ,c.[pf_trailerNum]
  ,b.[PartDesc]
  ,c.[pf_chargeBack]
  ,c.[pf_chargetoPlant]
 FROM [CNCTC-WEB01].[NOP_PR].[dbo].[Requests] a
   JOIN [CNCTC-WEB01].[NOP_PR].[dbo].[Parts] b on a.[RequestID] = b.[RequestID]
   JOIN [PHRIZ-WEBAPP01].[PFTracking].[dbo].[Tbl_PFExcel] c on  b.[PartNumber] like '%'+c.pf_id+'%'
 where a.[EntityName] like '%PTA' 
AND a.[RequestDate] between '2015-04-20 00:00:00.000' AND GETDATE()

1 个答案:

答案 0 :(得分:1)

您不需要创建临时表并添加临时列来删除重复项。单个查询就可以了。以下是使用CTE

的方法
CREATE PROCEDURE RemoveDuplicatesTempTable
AS
BEGIN

SET NOCOUNT ON;

WITH Cte AS(
    SELECT *,
        RN = RANK() OVER (
                PARTITION BY 
                    [PTAID]
                    ,[RequestID]
                    ,[RequestDate]
                    ,[ProvName]
                    ,[Amount]
                    ,[INorOUT]
                    ,[Supplier]
                    ,[Customer]
                    ,[Program]
                    ,[IssueDesc]
                    ,[Chargeable] 
                ORDER BY sno DESC) 
     From [PFTracking].[dbo].[TempTable]
)
DELETE FROM Cte WHERE RN > 1

END

您可以阅读有关存储过程here的更多信息。

从ASP.Net应用程序调用存储过程:

using (SqlConnection con = new SqlConnection(_connectionString))
{
    using (SqlCommand cmd = new SqlCommand("RemoveDuplicatesTempTable", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        con.Open();
        cmd.ExecuteNonQuery();
    }
}