Solved

Cannot find the object "@dbName"

Posted on 2009-05-15
5
620 Views
Last Modified: 2012-05-07
Can anyone tell me what I can do to make this work?  The stored procedure is altered successfully, but when I try to run it with the command to execute it I get the following error:

EXEC sbs_ClearAllData 'Account'

Msg 4902, Level 16, State 1, Procedure sbs_ClearAllData, Line 16
Cannot find the object "dbo.@dbName" because it does not exist or you do not have permissions.
ALTER PROCEDURE sbs_ClearAllData
	@dbName varchar(50)  <-- This is line 16, fyi
 
AS
BEGIN
 
	SET NOCOUNT ON;	
 
	ALTER TABLE [dbo].[@dbName] NOCHECK CONSTRAINT ALL;
 
	IF OBJECTPROPERTY(object_id('@dbName'), 'TableHasForeignRef') = 1  
		DELETE FROM [dbo].[@dbName]  
	else   
		TRUNCATE TABLE [dbo].[@dbName]
 
	ALTER TABLE [dbo].[@dbName] CHECK CONSTRAINT ALL
 
	IF OBJECTPROPERTY(object_id('@dbName'), 'TableHasIdentity') = 1   
	DBCC CHECKIDENT ('@dbName', RESEED, 0)
 
	-- Populate TransactionCategory
	IF (SELECT COUNT(*) FROM TransactionCategory) = 0
	BEGIN
		INSERT INTO TransactionCategory
			(Id, Code, Description, IsCredit)
		VALUES
			(NEWID(), 'D', 'Debit', 0)
		INSERT INTO TransactionCategory
			(Id, Code, Description, IsCredit)
		VALUES
			(NEWID(), 'C', 'Credit', 1)
	END
 
END

Open in new window

0
Comment
Question by:bakerOSU333
[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
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24399920
>ALTER TABLE [dbo].[@dbName] NOCHECK CONSTRAINT ALL;
you cannot run that without dynamic sql.


exec ('ALTER TABLE [dbo].[' + @dbName + '] NOCHECK CONSTRAINT ALL')

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24399927
DBCC CHECKIDENT '@dbName, RESEED, 0)   -- no quotes
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 24399946
Personally i would check you have permission for that server, not just read access
0
 

Author Comment

by:bakerOSU333
ID: 24400016
angellll,

how would I fix this section of the code now?  it's closer to working, but I need to make the dbName become a column name for the id property and here is the error I get with the code snip after that:

Msg 207, Level 16, State 1, Line 1
Invalid column name 'Account'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'TableHasForeignRef'.

EXEC ('IF OBJECTPROPERTY(object_id("' + @dbName + '"), "TableHasForeignRef") = 1  
            DELETE FROM [dbo].['+ @dbName + ']  
      else  
            TRUNCATE TABLE [dbo].[' + @dbName + ']')
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24400196
please try this:
EXEC ('IF OBJECTPROPERTY(object_id(''' + @dbName + '''), ''TableHasForeignRef'') = 1  
            DELETE FROM [dbo].['+ @dbName + ']  
      else  
            TRUNCATE TABLE [dbo].[' + @dbName + ']')

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 55
Upgrade SQL Server 2014 Standard Edition from SQL Server 2012 Standard Edition 14 37
SSIS GUID Variable 2 33
Need help with a query 14 37
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.
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

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