Solved

How to Truncate table if the table refered by the view

Posted on 2009-07-01
4
904 Views
Last Modified: 2012-05-07
Hi,
I got a error on my stored procedure " cannot tuncate a table if the table is refered by the view" how do I over come this?

Thanks,
Manjula
0
Comment
Question by:ken hanse
[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
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 100 total points
ID: 24758796
you have to use  the 'DELETE ' statement instead of truncate or else drop the view and recreate it after the TRUNCATE statement
0
 
LVL 17

Accepted Solution

by:
pssandhu earned 200 total points
ID: 24758797
I think you will have to drop the view before you can tuncate. Here is what I suggest -
1. Generate the CREATE VIEW script before you can drop the view. You can get the original script for all the views crated on a table from the information schema:
Select View_Definition
from INFORMATION_SCHEMA.VIEWS
where Table_Name = <TableName>

2. Then drop the view, truncate the table and run the scripts you got by running the above query.
Hope this helps.
P.
0
 
LVL 43

Assisted Solution

by:Eugene Z
Eugene Z earned 100 total points
ID: 24759714
" cannot tuncate a table if the table is refered by the view"?
are you sure it is not "table referenced by a FOREIGN KEY constraint"?
or
you try to delete table instead of truncate?

can you please post the part of the code where from you got error and copy\paste error too?
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 100 total points
ID: 24760771
Hope you use an indexed view instead of a normal view.

You cannot use TRUNCATE TABLE on tables that:

    * Are referenced by a FOREIGN KEY constraint.
    * Participate in an indexed view.
    * Are published by using transactional replication or merge replication.

Hence you have to either DELETE or drop and recreate the Indexed view in order to use your TRUNCATE statement.

Hope this helps.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

635 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