I am tasked with keeping several tables updated with information to be brought in from an external source. To this end, I've been hunting online for ways to pass table names as parameters, and all the answers are complex and/or throw errors (such as the below: "Incorrect syntax near 'Table' error shows)
CREATE PROCEDURE sp_Insert_Delta
-- Add the parameters for the stored procedure here
@tableName Table READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Delete rows in MIRROR database where ID exists in the DELTA database
Delete from [S1].[MIRROR].[dbo].@tableName
Where [ID] in (Select [ID] from [S2].[DELTAS].[dbo].@tableName)
-- Insert all deltas
Insert Into [S1].[MIRROR].[dbo].@tableName
Select * from [S2].[DELTAS].[dbo].@tableName
END
GO
This script works just fine when named explicitly, so how can I parameterize the table name?
Thank you,
Nate