Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"Could not find stored procedure" When Running Stored Procedure

Posted on 2008-06-10
3
Medium Priority
?
3,488 Views
Last Modified: 2010-04-21
When I create the stored procedure below, then run it by supplying the  2 parameters, I get the following Errors:
Msg 2812, Level 16, State 62, Procedure CEM_BulkOpsInsert, Line 15
Could not find stored procedure 'Delete From Case_IUD WHERE LEN(ID) > 4'.
Msg 2812, Level 16, State 62, Procedure CEM_BulkOpsInsert, Line 17
Could not find stored procedure ''.

If I hard code the second parameter @table and take out the dynamic SQL it works fine. What is wrong?
USE [xxx]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CEM_BulkOpsInsert] 
@tablename varchar(50),
@table varchar(100)
AS
BEGIN
 
	SET NOCOUNT ON;
 
Declare @error nvarchar(255)
Declare @subject1 varchar(200)
Declare @sql varchar(255)
 
exec SF_BulkOps 'Insert',@tablename,@table
Set @sql = 'Delete From ' + @table + ' WHERE LEN(ID) > 4'
exec @sql
Set @sql = 'select ' + @error + ' = COUNT(ID) From ' + @table + ' WHERE LEN(Error) > 4'
exec @sql
IF @error > 0
BEGIN
Set @subject1 = 'Errors Exist In ' + @table + ' For ' + @tablename
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Host',
    @recipients = 'host@xxx.com',
    @body = '',
    @subject = @subject1;
END
 
END

Open in new window

0
Comment
Question by:advlgx
3 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 21753771
try using parens:

exec(@sql)
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 21753810
use sp_executesql instead...in case you ever need to pass variables to it....also, it does a better job of caching the execution plan.

USE [xxx]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CEM_BulkOpsInsert]
@tablename varchar(50),
@table varchar(100)
AS
BEGIN
 
        SET NOCOUNT ON;
 
Declare @error nvarchar(255)
Declare @subject1 varchar(200)
Declare @sql varchar(255)
 
exec SF_BulkOps 'Insert',@tablename,@table
Set @sql = 'Delete From ' + @table + ' WHERE LEN(ID) > 4'
execute sp_executesql @sql
Set @sql = 'select ' + @error + ' = COUNT(ID) From ' + @table + ' WHERE LEN(Error) > 4'
exec @sql
IF @error > 0
BEGIN
Set @subject1 = 'Errors Exist In ' + @table + ' For ' + @tablename
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Host',
    @recipients = 'host@xxx.com',
    @body = '',
    @subject = @subject1;
END
 
END
0
 

Author Closing Comment

by:advlgx
ID: 31465868
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

886 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