Solved

"Could not find stored procedure" When Running Stored Procedure

Posted on 2008-06-10
3
3,402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 21753771
try using parens:

exec(@sql)
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

749 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