Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dynamic inserts

Author  Topic 

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2008-05-27 : 15:31:44
Hello All,

I have to create dynamic insert statements for the table. For example there are DevTableA and ProdTableA tables. I worte a SQL to get the new records added in the DevTableA but are not there in ProdTableA. The result gives me a list of rows. These tables have a column 'LanguageID' and 'LText'

The compare result has records only for LanguageID = 0. One I see the compare result. I am suppose to create insert statements for LanguageID = 1,2,5 and 6 and update the Ltext for those languages. The Ltext for other languages is in spreadsheet.

Can anyone advice me how to create the insert statements from the comapre result and add 4 more insert statements for LanguageID = 1,2,5 and 6 with their respective Ltext.

So far I thought I can create #table. Looks like I need more than 1 # table.

Thanks in advance
-S

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-28 : 01:31:12
Compare Result? Whats that? If your attempt is to insert the new records to ProdTable then isnt it enough to use Export/Import wizard to import the records from DevTable to an intermediate staging table in ProdServer and then compare it with ProdTable? What will be frequency at which you want the comparison to happen? If its a one time thing, i think you can use Export/Import.
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2008-05-28 : 11:45:42
I have to compare the data between development table and prod production table where LanguageID = 0. Insert only the missing rows in prod table from dev table. But It is not just the INSERT. I have to insert four more rows for each insert row found. These inserts will be for LanguageID 1,2,5,6. We have respective Ltext for these 1,2,5,6 LanguageIDs in excel spreadsheet. I have to insert these rows with the Ltext and LanguageID.

For example:
If the compare result had the following row
LanguageID LText
0 Are you sure?

I have to insert four rows for this insert + above insert

LanguageID LText
1 aaaaaa(German)
2 bbbbbb(France)
...

There is a excel spreadsheet
English German France
Are you sure aaaaaa bbbbbb

Teh Ltext column will be coming from the spreadsheet


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-28 : 12:28:47
quote:
Originally posted by sqlpal2007

I have to compare the data between development table and prod production table where LanguageID = 0. Insert only the missing rows in prod table from dev table. But It is not just the INSERT. I have to insert four more rows for each insert row found. These inserts will be for LanguageID 1,2,5,6. We have respective Ltext for these 1,2,5,6 LanguageIDs in excel spreadsheet. I have to insert these rows with the Ltext and LanguageID.

For example:
If the compare result had the following row
LanguageID LText
0 Are you sure?

I have to insert four rows for this insert + above insert

LanguageID LText
1 aaaaaa(German)
2 bbbbbb(France)
...

There is a excel spreadsheet
English German France
Are you sure aaaaaa bbbbbb

Teh Ltext column will be coming from the spreadsheet





You could use SSIS for this task. Using SSIS move the data from development table and excel spreadsheet to two staging tables on production. Then call a procedure to compare the first staging table and production table like this and insert extra records.It will be like this:-

CREATE PROC DataInsert
AS
BEGIN
DECLARE @Temp TABLE
(
TextField varchar(2000)
)


INSERT INTO ProdTableA
OUTPUT INSERTED.LText INTO @Temp
SELECT d.LanguageID,d.LText
FROM DevTableA_Staging d (staging table with data from dev server)
LEFT JOIN ProdTableA p
ON p.PK=d.PK
WHERE p.PK IS NULL
AND d.LanguageID=0

INSERT INTO ProdTableA
SELECT 1, German
FROM Excel_Staging e
INNER JOIN @Temp t
ON e.English=t.TextField

UNION ALL

SELECT 2, France
FROM Excel_Staging e
INNER JOIN @Temp t
ON e.English=t.TextField

UNION ALL
...


END
GO
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2008-05-28 : 13:27:37
Thanks Visakh16. I will try your suggestion
Go to Top of Page
   

- Advertisement -