I wanna create delete trigger. Idea is when user deletes the record from table trigger moves this record to another table (this table is copy has the same fields, types) But all this I want to do dinamic. I start so:
[code=sql]
ALTER TRIGGER [BackUpNotice]
ON [dbo].[module_notices]
FOR DELETE
AS
BEGIN
DECLARE @fieldname VARCHAR(100)
DECLARE @val VARCHAR(100)
DECLARE @b VARCHAR(MAX)
DECLARE get_fieldname CURSOR FOR
SELECT COLUMN_NAME FROM INFORMATION_SCH EMA.COLUMNS
WHERE (TABLE_NAME = 'module_notices ')
OPEN get_fieldname
FETCH NEXT FROM get_fieldname
WHILE @@FETCH_STATUS = 0
BEGIN
@val = @fieldname + ', ' + @val why this doesnt work? How can I generate fields and values?
FETCH NEXT FROM get_fieldname INTO @fieldname
--set @b = 'SELECT [' + @fieldname + '] FROM Deleted'
--here I need to have result from the query in variable, but I don't no how
END
CLOSE get_fieldname
DEALLOCATE get_fieldname
END [/code]
I want to generate fields and values and get query
INSERT INTO deleted.notices ( here fields ) VALUES ( here values )
Thank you a lot. And sorry form my English.
[code=sql]
ALTER TRIGGER [BackUpNotice]
ON [dbo].[module_notices]
FOR DELETE
AS
BEGIN
DECLARE @fieldname VARCHAR(100)
DECLARE @val VARCHAR(100)
DECLARE @b VARCHAR(MAX)
DECLARE get_fieldname CURSOR FOR
SELECT COLUMN_NAME FROM INFORMATION_SCH EMA.COLUMNS
WHERE (TABLE_NAME = 'module_notices ')
OPEN get_fieldname
FETCH NEXT FROM get_fieldname
WHILE @@FETCH_STATUS = 0
BEGIN
@val = @fieldname + ', ' + @val why this doesnt work? How can I generate fields and values?
FETCH NEXT FROM get_fieldname INTO @fieldname
--set @b = 'SELECT [' + @fieldname + '] FROM Deleted'
--here I need to have result from the query in variable, but I don't no how
END
CLOSE get_fieldname
DEALLOCATE get_fieldname
END [/code]
I want to generate fields and values and get query
INSERT INTO deleted.notices ( here fields ) VALUES ( here values )
Thank you a lot. And sorry form my English.
Comment