?
Solved

Not able to use a TRUNCATE command in

Posted on 2016-08-01
4
Medium Priority
?
91 Views
Last Modified: 2016-08-04
I am not able to create the below Sybase stored procedure. The Sybase error indicates that it does not like the line where
my TRUNCATE command is. The error message says incorrect syntax near @inTable. If I comment out that line where the TRUNCATE is, I can create the stored procedure. However I need to truncate the table. What is wrong with the line where my TRUNCATE statement is located? Can an expert help me out? What is odd is that I can run the truncate with in the command line if I actually
use the name of the table. Like TRUNCATE Table sampleTable

CREATE PROCEDURE dbo.spTruncateTable
@inTable VARCHAR(100) = NULL
AS
 BEGIN
   TRUNCATE TABLE @inTable
   PRINT 'Table Truncated'
 END
0
Comment
Question by:brgdotnet
[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
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Phil Davidson earned 2000 total points
ID: 41738329
I think that TRUNCATE cannot operate against a table in a loop based on a that same table.  TRUNCATE is a DDL command.  This means the definition of the table is eliminated.  DELETE is a DML command.  I think a loop like that can delete data from the tables.  But when you nest a truncate to eliminate a table created in the procedure, the Sybase database engine gets confused.  Delete the content of the table would be different.
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 41738409
I could use the delete command. But Sir, what loop are you talking about? The one which exists in the TRUNCATE function developed by Sybase? I think that is what you meant, because in the above code I am not using a loop Sir.
0
 
LVL 13

Expert Comment

by:alpmoon
ID: 41739669
The problem is using a variable in truncate command. It is not allowed. You can only do it through execute command:

execute ( 'TRUNCATE TABLE  '+@inTable)
0
 
LVL 13

Expert Comment

by:alpmoon
ID: 41739680
By the way I assumed that it is ASE, if you are trying it on ASA the syntax would be different. Let me know what product and version you are using if this syntax doesn't work.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

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