I have two databses, tempdblog and testdblog. I’m trying to figure out how, when i alter a table on tempdblog, that exact same command will be executed on testdblog, i don’t want the rows transfered i strictly want the columns.
Below is what i have atm from a site, i’ve tried to add a “USE testdblog” but it errors back at me about “a USE statement is not allowed…” as well as “must declare the scalar variable @test”.
The new column names could be anything, all i know is that it’s not a “add this column to the end of the table”, it’s more like “add this column just before userdef0 column”.
I store the SQL query it ran on the main database and try to re-execute it on the other table, it’s just a matter of finding out how to change databases.
USE tempdblog
GO
ALTER TRIGGER [db_LOG]
ON DATABASE
FOR ALTER_TABLE
AS
SET NOCOUNT ON
DECLARE @xEvent XML
DECLARE @tests nvarchar(MAX)
SET @xEvent = eventdata()
SET @tests = CONVERT(VARCHAR(MAX), @xEvent.query('data(/EVENT_INSTANCE/TSQLCommand/CommandText)'))
exec testdblog..sp_executesql @tests;
GO
You can’t have
GOcommands in there. Once you have a valid dynamic SQL statement constructed (you don’t right now), you should also try:Or just simply: