Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Error creating stored procedure w/ existing table

Author  Topic 

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2010-06-03 : 15:26:36
I have the following stored procedure that checks if a table exists and drops it. Then it Selects into a new table by the same name and adds indexes to it. The problem is that when I run the query to create the SP, I get this error:
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'MSDN_Alldata' in the database.

Here is the SP:
ALTER PROCEDURE CreateAndPopulate_MSDN_AllData AS

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSDN_Alldata]') AND type in (N'U'))
DROP TABLE [dbo].[MSDN_Alldata]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

select dt.* into MSDN_Alldata from (
select distinct d.* from (
select MSDN.Email, H.* from MSDN_Email MSDN
left join HR_NSV_PM H on H.Email_ID = MSDN.Email
) d
where nullif(d.emplid,'') is not null
union all
select 'MSDN email NOT Found - Name Match' as Email, h.* from (
select distinct MSDN.Email, MSDN.First_Name FN, MSDN.Last_Name LN, H.* from MSDN_Email MSDN
left join HR_NSV_PM H on H.Email_ID = MSDN.Email
where nullif(H.EMPLID,'') is null
) d
left join HR_NSV_PM h on h.LAST_NAME = d.LN and h.FIRST_NAME = d.FN
WHERE NULLIF(h.EMPLID,'') IS NOT NULL
) dt
order by CASE WHEN dt.Email Like 'MSDN%' THEN 1 ELSE 0 END, CASE WHEN nullif(dt.EMPLID,'') IS NULL THEN 1 ELSE 0 END, dt.LAST_NAME, dt.FIRST_NAME

CREATE INDEX idx_MSDN_AllData_EMPLID
ON dbo.MSDN_AllData
(
EMPLID
)
GO

CREATE INDEX idx_MSDN_AllData_EMAIL
ON dbo.MSDN_AllData
(
EMAIL
)
GO

CREATE INDEX idx_MSDN_AllData_LAST_NAME
ON dbo.MSDN_AllData
(
LAST_NAME
)
GO

CREATE INDEX idx_MSDN_AllData_FIRST_NAME
ON dbo.MSDN_AllData
(
FIRST_NAME
)
GO

CREATE INDEX idx_MSDN_AllData_AU
ON dbo.MSDN_AllData
(
AU
)
GO

CREATE INDEX idx_MSDN_AllData_AU_NAME
ON dbo.MSDN_AllData
(
AU_NAME
)
GO
This happens when I just create or alter the procedure. Ironically, the SP is actually created, but why the error? I didn't try to run it and even if I did, the script says to drop the table first anyway. Now, when I script this procedure (that was created evidently with errors), I get this:
USE [SoftwareReporting]
GO

/****** Object: StoredProcedure [dbo].[CreateAndPopulate_MSDN_AllData] Script Date: 06/03/2010 15:21:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CreateAndPopulate_MSDN_AllData]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N' -- Select into new table MSDN_AllData
-- All MSDN rows, All columns - includes Email matches and Name matches
-- name matches have duplicates as there are more than 1 name to match an EMPLID in HR
CREATE PROCEDURE [dbo].[CreateAndPopulate_MSDN_AllData] AS

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[MSDN_Alldata]'') AND type in (N''U''))
DROP TABLE [dbo].[MSDN_Alldata]
'
END
GO
It cut off most of the procedure. I will appreciate if someone can tell me what's happening and what to do about it.

Duane

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-03 : 15:48:37
It is cutting off the procedure due to the GO statement. GO means execute the previous batch, and so that's the end of your stored procedure. You'll need to remove all of the GO statements from your script for the parts that should be in the stored procedure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2010-06-03 : 16:33:15
It's amazing something simple like that can fix everything. I eliminated all the GO statements except the last one and it worked without a hitch. Thank you. I obviously don't totally understand the GO statements and their purpose. They seem to be everywhere and when you copy code from one place to another, they and a number of other things end up going with it. And if you don't understand these other parts, they are often the ones that cause the problems.

Duane
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-03 : 16:39:06
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -