• subscribe
January 24, 2002 12:00 AM

Executing TRUNCATE TABLE Against a Table on a Remote Server

SQL Server Pro
InstantDoc ID #23500
Downloads
23500.zip

I have a linked server called server02 properly configured on SQL Server 7.0 Service Pack 3 (SP3), and I want to truncate a table on a remote server called server01. When I try to execute the command

TRUNCATE TABLE server01.database01
.dbo.Tablename01

I get the error message that Figure 1 shows. How can I execute TRUNCATE TABLE against a table on a remote server?

TRUNCATE TABLE isn't a command that you can run directly by using the four-part name associated with a linked server. However, you can issue the TRUNCATE TABLE command against the linked server by using the sp_executesql stored procedure. Sp_executesql is designed primarily to help you parameterize a SQL query so that SQL Server can reuse the plan more easily. However, sp_executesql is also valuable when you're running commands against linked servers.

Listing 1 shows an example of how to use sp_executesql to execute SQL commands on a remote server even if the native commands don't directly support linked servers. Although this example demonstrates the execution of TRUNCATE TABLE, you can use this stored procedure to execute almost any type of SQL command. The sp_executesql stored procedure also supports flexible parameter substitution, although the example in Listing 1 doesn't require it. For more information about sp_executesql, see SQL Server Books Online (BOL).



ARTICLE TOOLS

Comments
  • Anonymous User
    8 years ago
    Nov 26, 2004

    I have the same problem - I want to truncate table through linked server, but, on Oracle database. I get the same error message, and sp_executesql cannot work now. How can I solve this problem?

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...