So here's the setup. I have two tables:
CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
CREATE TABLE dbo.TempFeesToRules2(FeeId int, Name varchar)
I have a third table called 'Fee' in the database that's already created. I want to populate dbo.TmpFeesToRules1 'Name' field with the DISTINCT 'Name' from 'Fee'. Would I do this like this?
INSERT INTO dbo.TmpFeesToRules1(Name, LookupId)
VALUES (SELECT DISTINCT Name FROM Fee, 0)
Then I want to use a cursor to loop through dbo.TmpFeesToRules1 and insert each of these rows into another table called 'Lookup', so those names would then have LookupId's assigned to them:
DECLARE db_cursor CURSOR FOR
SELECT Name
FROM dbo.TmpFeesToRules1
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @Name
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO dbo.Lookup (LookupType, LookupDesc)
VALUES ('FEE', @Name)
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
Then I want to come back to dbo.TmpFeesToRules1 and UPDATE it and insert those LookupId's for each one of the names. How do I do this?
Also, I don't think my SQL is entirely correct for everything else either? Can you guys verify this?