?
Solved

"Could not find stored procedure" When Running Stored Procedure

Posted on 2008-06-10
3
Medium Priority
?
3,446 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 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

719 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