July 15, 2013 at 11:29 am
Hello,
I am trying to truncate data from one server to the other (remote server)
it works from query analyzer like this:
exec RemoteServerName.DatabaseName.DBO.sp_executesql "truncate table DBO.tablename"
it does NOT work from query analyzer like this:
Declare @cmd varchar(2000)
set @cmd = @servername + '.' + @databasename + '_Archive'+ '.' + @schema + '.' + 'sp_executesql' + ' ' + '"' +'truncate table' + ' ' + @schema + '.' + @tablename + '"'
exec xp_cmdshell @cmd
Produced this error:
'RemoteServerName.DatabaseName.DBO.sp_executesql' is not recognized as an internal or external command,
operable program or batch file.
Please Help !
July 15, 2013 at 11:43 am
That's because xp_cmdshell expects an MS-DOS command and you're sending a T-SQL command.
July 15, 2013 at 12:59 pm
so how would i truncate data in my scenario through T-SQL command? Please...
July 15, 2013 at 1:07 pm
Just as you posted, no need to call xp_cmdshell.
July 15, 2013 at 2:11 pm
I ran exactly you mentioned and got this error
The RemoteServerName.DatabaseName.DBO.sp_executesql N'truncate table DBO.tablename' is not a valid identifier
i tried double quotes and single quote and same error above.
July 15, 2013 at 2:28 pm
I just tried it and it worked fine.
I can't replicate your error either for a missing linked server or a misdirection for the database. Maybe someone else can help you.
July 15, 2013 at 4:20 pm
If I understand correctly what you want:
DECLARE @sp_executesql nvarchar(1000)
DECLARE @sql nvarchar(MAX)
SELECT @sp_executesql =
quotename(@server) + '.' + quotename(@db) + '.sys.sp_executesql'
SELECT @sql = 'TRUNCATE TABLE ' + quotename(@schema) + '.' + quotename(@tablename)
EXEC @sp_executesql @sql
That is, one variable to define sp_executesql and one for the command itself.
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
July 16, 2013 at 9:15 am
Thank you for the try but it gave me the following error ๐
similar to the on earlier...
The name RemoteServerName.DatabaseName.DBO.sys.sp_executesql is not a valid identifier
Please advise.
July 16, 2013 at 9:39 am
You might get some mileage out of this:
DECLARE @SQLTruncate VARCHAR(200)
SET @SQLTruncate = 'TRUNCATE TABLE ' + @schema + '.' + @tablename
EXECUTE(@SQLTruncate) AT LinkedServerName -- no quotes around linkedservername
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 16, 2013 at 9:54 am
Hey Chris, does SQL Server consider that type of command a pass-through?
July 16, 2013 at 3:25 pm
imransi17 (2013-07-16)
Thank you for the try but it gave me the following error ๐similar to the on earlier...
The name RemoteServerName.DatabaseName.DBO.sys.sp_executesql is not a valid identifier
To whom was that in reply to? And can you post the exact code you used?
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
July 17, 2013 at 1:10 am
@Erin, yes, and it's not limited to a single statement. One such pass-thru command in the system I'm currently working on contains several INSERT and UPDATE statements protected by a manual transaction.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 17, 2013 at 12:04 pm
Thank you all of you guys and gals to assist. I finally got the solution to my above original issue.
Solution:
Declare @cmd varchar(2000)
set @cmd = 'EXEC ' + @servername + '.' + @databasename + '_Archive'+ '.' + @schema + '.' + 'sp_executesql @statement= N''truncate table ' + @schema + '.' + @tablename + ''''
exec (@cmd)
Again Thank you all,
Cheers,
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply