Solved

SP to find and replace data...

Posted on 2011-03-03
3
250 Views
Last Modified: 2012-05-11
On SQL 2000....  Experts, I am terrible in creating SP's...    I need help in converting the process below into a SP....


 
-----------------------------------------------------------------------------------------------
-- STEP1:  THIS CREATES TEMP TABLE 'X_TEMP_Rpt_Admission20110302'
-----------------------------------------------------------------------------------------------
exec USP_Rpt_Admission20110302 '
SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP# AND HPP.PPPATno = MDP.PJPAT#
WHERE (HPP.PPTYPE = ''IP'') 
AND (HPP.PPMTCD <> ''D'') 
AND (mdp.PJAFTP = ''2'') 
AND (HPP.ccYRMM BETWEEN DATENAME(year, DATEADD(m, - 24, CONVERT (varchar, GETDATE(), 112))) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month, DATEADD(m, - 0, CONVERT (varchar, GETDATE(), 112))) AS VarChar), 2) and DATENAME(year, DATEADD(m, - 1, CONVERT (varchar, GETDATE(), 112))) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month, DATEADD(m, - 1, CONVERT (varchar, GETDATE(), 112))) AS VarChar), 2))
AND (MDP.PDSPCL <> ''nur'') 
AND (HPP.ccGrpName = ''ELA'')
',
'ccYRMM',
'count(cntr)[]',
'PDSPCL,DoctorName'
-----------------------------------------------------------------------------------------------
-- STEP2:   I Need to find if records exist on Reporting Table using this criteria or similar...
-----------------------------------------------------------------------------------------------
SELECT COUNT(*) AS qty
FROM  dbo.Rpt_Admission24
GROUP BY RptYearMonth
HAVING (RptYearMonth = DATENAME(year, GETDATE()) + '-' + RIGHT('0' + CAST(DATEPART(month, DATEADD(m, - 1, GETDATE())) AS VarChar), 2))
-----------------------------------------------------------------------------------------------
-- STEP3: if NO critera exists... all i want to do is update with statement below...
-----------------------------------------------------------------------------------------------
insert into Rpt_Admission24 ([RptYearMonth]
      ,[PDSPCL]
      ,[DoctorName]
      ,[Month1]
      ,[Month2]
      ,[Month3]
      ,[Month4]
      ,[Month5]
      ,[Month6]
      ,[Month7]
      ,[Month8]
      ,[Month9]
      ,[Month10]
      ,[Month11]
      ,[Month12]
      ,[Month13]
      ,[Month14]
      ,[Month15]
      ,[Month16]
      ,[Month17]
      ,[Month18]
      ,[Month19]
      ,[Month20]
      ,[Month21]
      ,[Month22]
      ,[Month23]
      ,[Month24])
SELECT *
  FROM [ReportDB].[dbo].[X_TEMP_Rpt_Admission20110302] 
-----------------------------------------------------------------------------------------------
-- If records exist, I want to delete data then update
-----------------------------------------------------------------------------------------------
delete from Rpt_admission24
where (RptYearMonth = DATENAME(year, GETDATE()) + '-' + RIGHT('0' + CAST(DATEPART(month, DATEADD(m, - 1, GETDATE())) AS VarChar), 2))
GO

insert into Rpt_Admission24 ([RptYearMonth]
      ,[PDSPCL]
      ,[DoctorName]
      ,[Month1]
      ,[Month2]
      ,[Month3]
      ,[Month4]
      ,[Month5]
      ,[Month6]
      ,[Month7]
      ,[Month8]
      ,[Month9]
      ,[Month10]
      ,[Month11]
      ,[Month12]
      ,[Month13]
      ,[Month14]
      ,[Month15]
      ,[Month16]
      ,[Month17]
      ,[Month18]
      ,[Month19]
      ,[Month20]
      ,[Month21]
      ,[Month22]
      ,[Month23]
      ,[Month24])
SELECT *
  FROM [ReportDB].[dbo].[X_TEMP_Rpt_Admission20110302] 
GO

Open in new window

0
Comment
Question by:epicazo
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35032403
Check if this makes sense to you.  Also let me know what should be the input parameters if any.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE YourProcedureName
AS
BEGIN
	SET NOCOUNT ON;

	declare @qty int

	begin transaction

	-----------------------------------------------------------------------------------------------
	-- STEP1:  THIS CREATES TEMP TABLE 'X_TEMP_Rpt_Admission20110302'
	-----------------------------------------------------------------------------------------------
	exec USP_Rpt_Admission20110302 '
	SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
	FROM  dbo.VW_HPPATMFL HPP
	INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP# AND HPP.PPPATno = MDP.PJPAT#
	WHERE (HPP.PPTYPE = ''IP'') 
	AND (HPP.PPMTCD <> ''D'') 
	AND (mdp.PJAFTP = ''2'') 
	AND (HPP.ccYRMM BETWEEN DATENAME(year, DATEADD(m, - 24, CONVERT (varchar, GETDATE(), 112))) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month, DATEADD(m, - 0, CONVERT (varchar, GETDATE(), 112))) AS VarChar), 2) and DATENAME(year, DATEADD(m, - 1, CONVERT (varchar, GETDATE(), 112))) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month, DATEADD(m, - 1, CONVERT (varchar, GETDATE(), 112))) AS VarChar), 2))
	AND (MDP.PDSPCL <> ''nur'') 
	AND (HPP.ccGrpName = ''ELA'')
	',
	'ccYRMM',
	'count(cntr)[]',
	'PDSPCL,DoctorName'

	-----------------------------------------------------------------------------------------------
	-- STEP2:   I Need to find if records exist on Reporting Table using this criteria or similar...
	-----------------------------------------------------------------------------------------------
	SELECT @qty = COUNT(*)
	FROM  dbo.Rpt_Admission24
	GROUP BY RptYearMonth
	HAVING (RptYearMonth = DATENAME(year, GETDATE()) + '-' + RIGHT('0' + CAST(DATEPART(month, DATEADD(m, - 1, GETDATE())) AS VarChar), 2))

	if @qty = 0 begin
		-----------------------------------------------------------------------------------------------
		-- STEP3: if NO critera exists... all i want to do is update with statement below...
		-----------------------------------------------------------------------------------------------
		insert into Rpt_Admission24 ([RptYearMonth]
			  ,[PDSPCL]
			  ,[DoctorName]
			  ,[Month1]
			  ,[Month2]
			  ,[Month3]
			  ,[Month4]
			  ,[Month5]
			  ,[Month6]
			  ,[Month7]
			  ,[Month8]
			  ,[Month9]
			  ,[Month10]
			  ,[Month11]
			  ,[Month12]
			  ,[Month13]
			  ,[Month14]
			  ,[Month15]
			  ,[Month16]
			  ,[Month17]
			  ,[Month18]
			  ,[Month19]
			  ,[Month20]
			  ,[Month21]
			  ,[Month22]
			  ,[Month23]
			  ,[Month24])
		SELECT *
		  FROM [ReportDB].[dbo].[X_TEMP_Rpt_Admission20110302] 
	end else begin
		-----------------------------------------------------------------------------------------------
		-- If records exist, I want to delete data then update
		-----------------------------------------------------------------------------------------------
		delete from Rpt_admission24
		where (RptYearMonth = DATENAME(year, GETDATE()) + '-' + RIGHT('0' + CAST(DATEPART(month, DATEADD(m, - 1, GETDATE())) AS VarChar), 2))

		insert into Rpt_Admission24 ([RptYearMonth]
			  ,[PDSPCL]
			  ,[DoctorName]
			  ,[Month1]
			  ,[Month2]
			  ,[Month3]
			  ,[Month4]
			  ,[Month5]
			  ,[Month6]
			  ,[Month7]
			  ,[Month8]
			  ,[Month9]
			  ,[Month10]
			  ,[Month11]
			  ,[Month12]
			  ,[Month13]
			  ,[Month14]
			  ,[Month15]
			  ,[Month16]
			  ,[Month17]
			  ,[Month18]
			  ,[Month19]
			  ,[Month20]
			  ,[Month21]
			  ,[Month22]
			  ,[Month23]
			  ,[Month24])
		SELECT *
		  FROM [ReportDB].[dbo].[X_TEMP_Rpt_Admission20110302] 
	end

	commit
END
GO

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35032458
TRY THIS
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE procName 
AS
BEGIN
  EXEC USP_Rpt_Admission20110302 '
		SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
		FROM  dbo.VW_HPPATMFL HPP
		INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP# AND HPP.PPPATno = MDP.PJPAT#
		WHERE (HPP.PPTYPE = ''IP'') 
		AND (HPP.PPMTCD <> ''D'') 
		AND (mdp.PJAFTP = ''2'') 
		AND (HPP.ccYRMM BETWEEN DATENAME(year, DATEADD(m, - 24, CONVERT (varchar, GETDATE(), 112))) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month, DATEADD(m, - 0, CONVERT (varchar, GETDATE(), 112))) AS VarChar), 2) and DATENAME(year, DATEADD(m, - 1, CONVERT (varchar, GETDATE(), 112))) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month, DATEADD(m, - 1, CONVERT (varchar, GETDATE(), 112))) AS VarChar), 2))
		AND (MDP.PDSPCL <> ''nur'') 
		AND (HPP.ccGrpName = ''ELA'')
		',
		'ccYRMM',
		'count(cntr)[]',
		'PDSPCL,DoctorName'
	
	IF EXISTS(SELECT COUNT(*) AS qty
		FROM  dbo.Rpt_Admission24
		GROUP BY RptYearMonth
		HAVING (RptYearMonth = DATENAME(year, GETDATE()) + '-' + RIGHT('0' + CAST(DATEPART(month, DATEADD(m, - 1, GETDATE())) AS VarChar), 2)))
	BEGIN
		DELETE FROM Rpt_admission24
		WHERE (RptYearMonth = DATENAME(year, GETDATE()) + '-' + RIGHT('0' + CAST(DATEPART(month, DATEADD(m, - 1, GETDATE())) AS VarChar), 2))

		INSERT INTO Rpt_Admission24 ([RptYearMonth]
			  ,[PDSPCL]
			  ,[DoctorName]
			  ,[Month1]
			  ,[Month2]
			  ,[Month3]
			  ,[Month4]
			  ,[Month5]
			  ,[Month6]
			  ,[Month7]
			  ,[Month8]
			  ,[Month9]
			  ,[Month10]
			  ,[Month11]
			  ,[Month12]
			  ,[Month13]
			  ,[Month14]
			  ,[Month15]
			  ,[Month16]
			  ,[Month17]
			  ,[Month18]
			  ,[Month19]
			  ,[Month20]
			  ,[Month21]
			  ,[Month22]
			  ,[Month23]
			  ,[Month24])
		SELECT *
		  FROM [ReportDB].[dbo].[X_TEMP_Rpt_Admission20110302] 
	END
	ELSE
	BEGIN
	  INSERT INTO Rpt_Admission24 ([RptYearMonth]
			  ,[PDSPCL]
			  ,[DoctorName]
			  ,[Month1]
			  ,[Month2]
			  ,[Month3]
			  ,[Month4]
			  ,[Month5]
			  ,[Month6]
			  ,[Month7]
			  ,[Month8]
			  ,[Month9]
			  ,[Month10]
			  ,[Month11]
			  ,[Month12]
			  ,[Month13]
			  ,[Month14]
			  ,[Month15]
			  ,[Month16]
			  ,[Month17]
			  ,[Month18]
			  ,[Month19]
			  ,[Month20]
			  ,[Month21]
			  ,[Month22]
			  ,[Month23]
			  ,[Month24])
		SELECT *
		  FROM [ReportDB].[dbo].[X_TEMP_Rpt_Admission20110302] 
	END
END
GO

Open in new window

0
 

Author Closing Comment

by:epicazo
ID: 35061504
Sorry for da delay.   Thank u experts!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now