Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Truncate table if the table refered by the view

Posted on 2009-07-01
4
Medium Priority
?
906 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 300 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 600 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 300 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 300 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

664 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