Solved

Not able to use a TRUNCATE command in

Posted on 2016-08-01
4
53 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
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Phil Davidson earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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