annethorne
Programmer
Hi SQL Developer Friends,
I would like implement the following techniques in some SQL scripts:
1) Before updating a table, copy it into a new table that has the same table name, appended with an underscore and the datetime. For example if the name of the table was Browsers then I would copy the contents of that table into a new table named Browsers_20120512_23:35 or something like that... Right now I am doing this... but I would prefer the backup table to have the datetime in the name:
2) I would like to include in some scripts a loop so that different data could be inserted or updated using the same pattern pattern. For example, below, is a portion of the of script I am now writing... which involves a lot of cutting and pasting. I'd like to write a method once and then just have a loop. Notice that the only difference between the first code block and the second is that in the first the CountryID = 1, and in the second, the CountryID = 11. I have other scripts where more than one variable are different... but this should give you a general idea of what I'm looking for:
Thank you so much for any guidance that any one can give.
Anne
I would like implement the following techniques in some SQL scripts:
1) Before updating a table, copy it into a new table that has the same table name, appended with an underscore and the datetime. For example if the name of the table was Browsers then I would copy the contents of that table into a new table named Browsers_20120512_23:35 or something like that... Right now I am doing this... but I would prefer the backup table to have the datetime in the name:
SQL:
SELECT * INTO _overriderules_bak FROM OverrideRules;
2) I would like to include in some scripts a loop so that different data could be inserted or updated using the same pattern pattern. For example, below, is a portion of the of script I am now writing... which involves a lot of cutting and pasting. I'd like to write a method once and then just have a loop. Notice that the only difference between the first code block and the second is that in the first the CountryID = 1, and in the second, the CountryID = 11. I have other scripts where more than one variable are different... but this should give you a general idea of what I'm looking for:
SQL:
SET IDENTITY_INSERT OverrideRules ON
declare @overrideId as int;
--Obtain max id from OverrideRules (there is no identity property)
SELECT @overrideId = max([Override_id]) + 1 FROM [OverrideRules]
--OverrideRules
IF EXISTS (SELECT * FROM OverrideRules WHERE Override_notes='RegistrationSKU' and CountryID = '1')
BEGIN
UPDATE [OverrideRules]
SET [Override_notes]='RegistrationSKU',[Category]='Registration',[CountryID]='1'
WHERE Override_notes='RegistrationSKU' and CountryID = '1'
END
ELSE
BEGIN
INSERT INTO [OverrideRules]
([Override_id],[Override_notes],[Category],[CountryID])
VALUES(@overrideId,'RegistrationSKU','Registration','1')
set @overrideId = @overrideId + 1
END
IF EXISTS (SELECT * FROM OverrideRules WHERE Override_notes='RegistrationSKU' and CountryID = '11')
BEGIN
UPDATE [OverrideRules]
SET [Override_notes]='RegistrationSKU',[Category]='Registration',[CountryID]='11'
WHERE Override_notes='RegistrationSKU' and CountryID = '11'
END
ELSE
BEGIN
INSERT INTO [OverrideRules]
([Override_id],[Override_notes],[Category],[CountryID])
VALUES(@overrideId,'RegistrationSKU','Registration','11')
set @overrideId = @overrideId + 1
END
...
...
...
SET IDENTITY_INSERT OverrideRules OFF
Thank you so much for any guidance that any one can give.
Anne