Share via

Call another database table from another database sp

Arutprakasam 261 Reputation points
2023-11-29T02:11:00.7966667+00:00

Hi @everyone , please help me resolve my requirement, the requirement is to execute the truncate query from A database sp to B database table in both On Premises and cloud. Please share your valuable solution. And that table was in this format dbo.table1.

Azure SQL Database
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Anonymous
2023-11-29T03:02:31.6666667+00:00

Hi @Arutprakasam

If I understand correctly, you can try this.

CREATE PROCEDURE truncate_table
@dbname VARCHAR(max),
@schemaname VARCHAR(max),
@tablename VARCHAR(max)
AS
BEGIN
  DECLARE @SQL VARCHAR(MAX) = ''
  SET @SQL = 'TRUNCATE TABLE [' + @dbname + '].[' + @schemaname + '].[' + @tablename + ']'
  EXEC (@SQL)
END

EXEC truncate_table 'databaseB','dbo','table1';

Best regards,

Percy Tang

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.