Solved

"Could not find stored procedure" When Running Stored Procedure

Posted on 2008-06-10
3
3,366 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 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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.​
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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