0

Here is the SQL:

CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)

INSERT INTO dbo.TmpFeesToRules1(Name, LookupId)
SELECT DISTINCT Name, 0 FROM Lending.Fee
UNION SELECT DISTINCT Name, 0 FROM Lending.ApplicationFee

INSERT INTO dbo.tblLookup (LookupType, LookupCode, LookupDesc, EditFlag, DeletedFlag, DefaultFlag)
SELECT 'FEE_CODE', Name,  Name, 0, 0, 0
FROM dbo.TmpFeesToRules1

UPDATE dbo.TmpFeesToRules1
SET
    LookupId = L.LookupID
FROM
    dbo.tblLookup L
INNER JOIN dbo.TmpFeesToRules1 T ON T.Name = L.LookupDesc
WHERE
    L.LookupType = 'FEE_CODE'

CREATE TABLE dbo.TmpFeesToRules2(FeeId int, Name varchar)

INSERT INTO dbo.TmpFeesToRules2(FeeId, Name)
SELECT FeeId, Name FROM Lending.ApplicationFee

GO

UPDATE Lending.ApplicationFee 
SET Lending.ApplicationFee.FeeTypeId = T.LookupId
FROM dbo.TmpFeesToRules2 TT
INNER JOIN dbo.TmpFeesToRules1 T ON T.Name = TT.Name
WHERE Lending.ApplicationFee.FeeId = TT.FeeId

DROP TABLE dbo.TmpFeesToRules1
DROP TABLE dbo.TmpFeesToRules2

Here is the error:

Invalid object name 'dbo.TmpFeesToRules2'.
Execute failed, return code: 1

Any ideas?

2 Answers 2

6

Nothing to do with Go or semicolons,

You are updating Lending.ApplicationFee but it doesn't appear in the FROM clause.

UPDATE Lending.ApplicationFee 
SET Lending.ApplicationFee.FeeTypeId = T.LookupId
FROM dbo.TmpFeesToRules2 TT
INNER JOIN dbo.TmpFeesToRules1 T ON T.Name = TT.Name
WHERE Lending.ApplicationFee.FeeId = TT.FeeId

I guess this should be something like

UPDATE Lending.ApplicationFee 
SET Lending.ApplicationFee.FeeTypeId = T.LookupId
FROM Lending.ApplicationFee join dbo.TmpFeesToRules2 TT ON <Some Condition>
INNER JOIN dbo.TmpFeesToRules1 T ON T.Name = TT.Name
WHERE Lending.ApplicationFee.FeeId = TT.FeeId
Sign up to request clarification or add additional context in comments.

6 Comments

And I think this is where the error is :) in spite of all the CREATE/INSERT/GO debate
+1 I've deleted my input into that debate to avoid clouding the answer.
You should get a badge for that I think!
@Martin I didn't have enough votes to get that "delete your own post" badge, but up-voting you gave me Civic Duty lol, btw you don't work in Manchester do you?
Nope, no Manchester connections. It seems there's quite a few Martin Smiths about.
|
2

Instead of

CREATE TABLE dbo.TmpFeesToRules2(FeeId int, Name varchar)

INSERT INTO dbo.TmpFeesToRules2(FeeId, Name)
SELECT FeeId, Name FROM Lending.ApplicationFee

you could just do:

SELECT FeeId, Name 
INTO dbo.TmpFeesToRules2 
FROM Lending.ApplicationFee

That will create and populate the table in one step

1 Comment

I don't think so. And I don't think the problem is in the GO. GO is not a SQL command per se, it just tells the tool to send the code to the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.