Solved

How to Truncate table if the table refered by the view

Posted on 2009-07-01
4
902 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

752 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