November 8, 2010 at 6:41 am
Hello
An SQL brainstorm idea .. but wanting some reasurance ..
I am working on a third party database product and I need to create a new record from an existing record, but without specifyiing column names to avoid upgrade issues (a new field name could be added by the vendor).
The table has an auto Identity column Code
I have crafted some cunning SQL that I hope will avoid any issues relating to new/dropped columns;
ALTER PROCEDURE up_Document_CopyStep (@DocCode int, @NewDocName VarChar(500))
AS
BEGIN
-- 1/ Get the existing row data
SELECT * INTO #Temp
FROM dbo.Dm_Documents WHERE Code = @DocCode
-- 2/ Remove Identity Column
ALTER TABLE #Temp DROP COLUMN Code
-- 3/ Set new name
UPDATE #Temp SET Name = @NewDocName
-- 4/ Insert copy record
INSERTINTO dbo.Dm_Documents
SELECT * FROM #Temp
END
GO
EXEC up_Document_CopyStep 1502, 'sys_test2'
This seems to work nicely, so no maintenance problems?
Is there a chance this might go wrong / am I missing something?
C# Gnu
____________________________________________________

November 8, 2010 at 9:05 am
Lets pray vendor doesn't drop or change the name of "NAME" column 😀
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.November 8, 2010 at 9:08 am
PaulB-TheOneAndOnly (11/8/2010)
Lets pray vendor doesn't drop or change the name of "NAME" column 😀
Yep that would c#Screw it all right:-D
So no probs otherwise then ?
C# Gnu
____________________________________________________

November 8, 2010 at 12:35 pm
C# Screw (11/8/2010)
PaulB-TheOneAndOnly (11/8/2010)
Lets pray vendor doesn't drop or change the name of "NAME" column 😀Yep that would c#Screw it all right:-D
So no probs otherwise then ?
That's what I would call a very creative solution. congrats!
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.November 8, 2010 at 1:34 pm
PaulB-TheOneAndOnly (11/8/2010)
C# Screw (11/8/2010)
PaulB-TheOneAndOnly (11/8/2010)
Lets pray vendor doesn't drop or change the name of "NAME" column 😀Yep that would c#Screw it all right:-D
So no probs otherwise then ?
That's what I would call a very creative solution. congrats!
Cheers Paul
the bit that is very interesting I think is the fact that the sql engine gets the "INSERT INTO dm_documents " (Part 4) columns all in the right order. This being mostly due to me creating the #temp structure initially from the exact structure of dm_documents.
What is exceptionally interesting (to a sad act like me) is the fact the sql engine still manages to get the columns in the right order even when I have dropped one of the columns. [It would be] Nice to understand how SQL engine has achieved that:ermm:
C# Gnu
____________________________________________________

Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply