I am working with a legacy database at my company that has a rather strange back-end data model... There is a master table that looks sort of like this:
ID NAME
1 Customer Item
2 Internal Thing
This design is fine (looks somewhat conventional), but then there are related tables that essentially have a schema template, and then when a new item is added, a series of tables is created with the new record ID Field concatenated onto the end of it (ie, TABLETEMPLATE_1, TABLETEMPLATE_2).
Here is my dilemma:
We've since developed a system that stores data to a MUCH higher magnitude, with a more conventional table design (there are no dynamic tables; all tables and constraints exist in the data model, and this form of relationship is handled via foreign key constraints). The problem is that there are some UI elements that haven't been completely developed in the new system yet, and we need to perform what is basically an ETL Procedure on this legacy model. I'd LIKE to do it as a SQL Stored Procedure, but I'm having a hard time figuring out how to manage the inserts into the legacy model without blatantly writing SQL in my procedure.
Does anyone know of a way that I could define a temporary table, and then reference the table in an EXEC statement?
Thanks!
check this:
CREATE PROCEDURE [dbo].[GetAccounts]
@AccountID BIGINT,
@Result INT OUT,
@ErrorMessage VARCHAR(255) OUT
AS
BEGIN
SET NOCOUNT ON;
SET @Result = 0
SET @ErrorMessage = ''
DECLARE @tmp_Accounts TABLE (
AccountId BIGINT,
AccountName VARCHAR(50),
...
)
INSERT INTO @tmp_Accounts ([AccountId], [AccountName]...
)
SELECT AccountID, AccountName
FROM Accounts
WHERE ...
IF @@Rowcount = 0
BEGIN
SET @ErrorMessage = 'No accounts found.'
SET @Result = 0
RETURN @Result
END
ELSE
BEGIN
SET @Result = 1
SELECT *
FROM @tmp_Accounts
END
Just a quick update on this... Due to the nature of needing to extract some information, compare it, and then update the target, the only way that I was able to figure out how to do this was by dynamically writing the SQL code to extract what I needed into a temp table (#table, not @table) from the legacy system, and then updating the temp table in a batch (this helped prevent me from needing as many cursors as I we've used for this previously). This was inside a cursor, and I just truncated the table at the head of the cursor while loop.
One other thing: when generating the SQL, as I suspected, table variables (@table) were inaccessible by the generated SQL, which meant I had to use temp tables (#table). SQL Server threw a fit about having table variables that weren't declared in the generated SQL being referenced in the generated SQL, whereas temp tables (#table) last for the lifetime of the connection, and persist between batches (so a job could access the same data after using the 'GO' keyword).
Sorry.. Seems I didn't provide enough of a description the first time. My apologies for that one.
Thanks all!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.